This type of problem is often encountered. Now I will summarize the code I have written in the past two years, which will be much more convenient for programming in the future.
Document contains two arrays, a simple point. Each element is a string, a complex point. Each element is an object and has two attributes: Address and status.
> db.schedule.findOne({"_id" : ObjectId("51e930f92291307bb05a1a84")}){"_id" : ObjectId("51e930f92291307bb05a1a84"),"direction" : 2,"messages" : ["m1","m2","m3"],"receivers" : [{"address" : "000000000020","status" : "waiting"},{"address" : "000000000018","status" : "waiting"}],"speed" : 3,"start_time" : "1374236921","stay_time" : 1,"user_id" : ObjectId("518b7fc5117e87bce28f2444")}
The C ++ code is as follows:
This function fills in a simple array and uses bsonarraybuilder. Note that it demonstrates the use of the insert method.
string AddScheduledMessage(ScheduledMessage const& m) { shared_ptr<mongo_session> mongo_session = mongo_session_factory::get_session(); BSONObjBuilder data_builder; OID _id = OID::gen(); data_builder.append("_id", _id); data_builder.append("user_id", mongo::OID(m.user_id)); data_builder.append("start_time", m.start_time); data_builder.append("direction", m.direction); data_builder.append("speed", m.speed); data_builder.append("stay_time", m.stay_time); BSONArrayBuilder msgs_builder; size_t size = m.msgs.size(); for (size_t i = 0; i < size; ++i) { msgs_builder.append(m.msgs[i]); } data_builder.append("messages", msgs_builder.arr()); mongo_session->get().insert("db.schedule", data_builder.obj()); return _id.str();}
The second function adds a complex array and demonstrates the use of the update method. This method assumes that the receivers array does not exist, and uses the $ set command to set it.
void AssignScheduledMessages(ScheduledMessages & ms) { shared_ptr<mongo_session> mongo_session = mongo_session_factory::get_session(); vector<shared_ptr<ScheduledMessage> >::iterator itor, last = ms.values.end(); for (itor = ms.values.begin(); itor != last; ++itor) { shared_ptr<ScheduledMessage> m = *itor; BSONObjBuilder condition; condition.append("user_id", OID(m->user_id)); condition.append("_id", OID(m->id)); BSONArrayBuilder arr_builder; size_t size = m->receivers.size(); for (size_t i = 0; i < size; ++i) { BSONObjBuilder rec; rec.append("address", m->receivers[i]->address); rec.append("status", m->receivers[i]->status); arr_builder.append(rec.obj()); } BSONObjBuilder recs; recs.append("receivers", arr_builder.arr()); BSONObjBuilder set; set.append("$set", recs.obj()); mongo_session->get().update("db.schedule", mongo::Query(condition.obj()), set.obj()); }}
It is more complicated. Modify the status = "sending" of the element whose address is 00... 20 ". For details, refer:
Http://stackoverflow.com/questions/9247007/mongodb-update-the-specific-element-from-subarray
The following shows the JavaScript code:
db.schedule.update({"_id" : ObjectId("51e930f92291307bb05a1a84"), "receivers": {$elemMatch: {"address": "000000000020"}}}, {$set: {"receivers.$.status": "sending"}})> db.schedule.findOne(){"_id" : ObjectId("51e930f92291307bb05a1a84"),"direction" : 2,"messages" : ["m1","m2","m3"],"receivers" : [{"address" : "000000000020","status" : "sending"},{"address" : "000000000018","status" : "waiting"}],"speed" : 3,"start_time" : "1374236921","stay_time" : 1,"user_id" : ObjectId("518b7fc5117e87bce28f2444")}
$ This is a placeholder, indicating the first array element to be found. Document: http://docs.mongodb.org/manual/core/update/#array
Name |
Description |
$ |
Acts as a placeholder to update the first element that matches the query condition in an update. |
C ++ code:
void SaveScheduleMessageStatus(string const& message_id, string const& address, string const& status) { shared_ptr<MongoSession> mongo_session = MongoSessionFactory::GetSession(); BSONObjBuilder condition; condition.append("_id", OID(message_id)); BSONObjBuilder con1; con1.append("address", address); BSONObjBuilder con2; con2.append("$elemMatch", con1.obj()); condition.append("receivers", con2.obj()); BSONObjBuilder recs; recs.append("receivers.$.status", status); BSONObjBuilder set; set.append("$set", recs.obj()); mongo_session->GetDBClientBase().update("db.schedule", mongo::Query(condition.obj()), set.obj());}