MongoDB:如何将这个散列添加到集合中的每个文档中

MongoDB: How to add this hash to every document in a collection

本文关键字:集合 文档 添加 MongoDB      更新时间:2023-09-26

我需要向users集合中的每个文档添加这样的哈希:

"authorization" : {
        "_id" : ObjectId("52712baabe40ac667d000001"),
        "all" : "true",
        "boxscore" : "false",
        "created_at" : ISODate("2013-10-30T15:54:18.397Z"),
        "depth_charts" : "false",
        "fantasy_news" : "false",
        "injuries" : "false",
        "matchups" : "false",
        "news" : "false",
        "odds" : "false",
        "play_by_play" : "false",
        "previews" : "false",
        "recaps" : "false",
        "schedule" : "false",
        "standings" : "false",
        "statistics" : "false",
        "updated_at" : ISODate("2013-11-12T21:47:13.808Z")
    }

一些users已经有了这个散列,但不是所有的。如果现有用户已经嵌入了文档,可以覆盖他们。有没有办法在MongoDB的控制台中使用一些JavaScript来完成这项工作?

我还应该指出,这是一份嵌入的文件。因此,对于users集合中的每个文档,ObjectId应该是唯一的。

db.users.update({}, {$set: {authorization:  {
        "_id" : ObjectId(),
        "all" : "true",
        "boxscore" : "false",
        "created_at" : ISODate("2013-10-30T15:54:18.397Z"),
        "depth_charts" : "false",
        "fantasy_news" : "false",
        "injuries" : "false",
        "matchups" : "false",
        "news" : "false",
        "odds" : "false",
        "play_by_play" : "false",
        "previews" : "false",
        "recaps" : "false",
        "schedule" : "false",
        "standings" : "false",
        "statistics" : "false",
        "updated_at" : ISODate("2013-11-12T21:47:13.808Z")
    }}}, {multi: true})