Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.1k views
in Technique[技术] by (71.8m points)

mongodb - Change all my documents nested document value

In mongos shell how would I go through and change every document in reviews.category to "category 2"

My Documents Structure:

{
   "_id": ObjectId("4fb3f443b1445d24fc000000"),
   "reviews": {
     "0": {
       "category": "category 1"
    },
     "1": {
       "category": "category 1"
    },
     "2": {
       "category": "category 1"
    },
     "3": {
       "category": "category 1"

    } 
  }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You will have to do this yourself in your application code, by querying the document, and looping over all of your nested documents; and then save it back to MongoDB.

In order to prevent race conditions with this, please have a look at the section compare and swap at http://www.mongodb.org/display/DOCS/Atomic+Operations

There is currently an open ticket for this to add this functionality to MongoDB. You might want to up-vote it: https://jira.mongodb.org/browse/SERVER-1243


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...