MongoDB:如何将此哈希添加到集合中的每个文档

时间:2013-12-16 19:42:30

标签: javascript mongodb

我需要在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来完成这项工作?

我还应该注意,这是一个嵌入式文档。因此,对于ObjectId集合中的每个文档,users应该是唯一的。

1 个答案:

答案 0 :(得分:1)

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})