简体   繁体   中英

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:

"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. 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?

I should also note, that this is an embedded document. So ObjectId should be unique for each document in the users collection.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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