请问下在mongodb中的push如何定位?具体如下

mongodb中存储着类似结构的数据:
{
_id:"10001",
array:[
{sub_id:"1",sub_array:["text11","text12","text13"]},
{sub_id:"2",sub_array:["text21","text22","text23"]},
{sub_id:"3",sub_array:["text31","text32","text33"]}
]
}

直接向array中添加内容的话,很简单,利用$push操作符即可,
但如果我想往sub_id为2的sub_array中push若干个字符串,应该如何定位?

慕后森
浏览 268回答 1
1回答

LEATH

定位使用$elemMatch查找。参考案例:exports.updateReply2Async = function (opts) {var results = {error_code: -1, error_msg: "error"};var suggestID = helper.toObjectID(opts.doc.suggestID);var filter = {_id: suggestID,"replies":{ $elemMatch:{rid:opts.doc.rid,replyname:'1100100'}}};var update = {$set: {"replies.$.replycontent": opts.doc.replynewcontent,"replies.$.reply_at":new Date()}};return opts.dbs.csdb.collection("suggests").findOneAndUpdateAsync(filter, update, {upsert: false}).timeout(opts.configs.timeoutnormal).then(function (value) {if (value) {results.error_code = 0;results.error_msg = "ok";results.suggest = value;} else {results.error_code = 20002;results.error_msg = "修改回复数据失败";}return results;});};
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

MongoDB
Oracle