简体   繁体   English

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

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

I need to add a hash like this to every document in my users collection: 我需要向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")
    }

Some users already have this hash, but not all of them. 一些users已经具有此哈希,但不是全部。 It's okay to overwrite existing users if they already have the document embedded. 如果现有用户已经嵌入文档,则可以覆盖它们。 Is there a way in MongoDB's console using some JavaScript in order to get this done? MongoDB的控制台中是否有某种方法可以使用一些JavaScript来完成此任务?

I should also note, that this is an embedded document. 我还应该注意,这是一个嵌入式文档。 So ObjectId should be unique for each document in the users collection. 因此,对于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})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM