Mongodb怎样更新像这样的文档,数组里面有子文档
展开全部
更新数组字段使用$push关键词
提供参考代码:使用node.js
var filter;
if (paramtype === 1) {
filter = {helpapiuuid: helpapiuuid, "requests.paramid": opts.paramid};
} else {
filter = {helpapiuuid: helpapiuuid, "responses.paramid": opts.paramid};
}
var update;
if (paramtype === 1) {
update = {
$set: {
"requests.$.param": opts.param,
"requests.$.type": opts.desc,
"requests.$.desc": opts.desc,
"requests.$.default": opts.default
}
};
} else {
update = {
$set: {
"responses.$.param": opts.param,
"responses.$.type": opts.desc,
"responses.$.desc": opts.desc,
"responses.$.default": opts.default
}
};
}
return publicdb.collection(colname).findOneAndUpdateAsync(filter, update, {upsert: false})
提供参考代码:使用node.js
var filter;
if (paramtype === 1) {
filter = {helpapiuuid: helpapiuuid, "requests.paramid": opts.paramid};
} else {
filter = {helpapiuuid: helpapiuuid, "responses.paramid": opts.paramid};
}
var update;
if (paramtype === 1) {
update = {
$set: {
"requests.$.param": opts.param,
"requests.$.type": opts.desc,
"requests.$.desc": opts.desc,
"requests.$.default": opts.default
}
};
} else {
update = {
$set: {
"responses.$.param": opts.param,
"responses.$.type": opts.desc,
"responses.$.desc": opts.desc,
"responses.$.default": opts.default
}
};
}
return publicdb.collection(colname).findOneAndUpdateAsync(filter, update, {upsert: false})
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询