Mongoose 具有两个条件的数组的嵌套对象元素的findOneAndUpdate
我想将数组的嵌套对象元素的“displayTitle”更新为“12345”,其中topten=“under 50000”findOneAndUpdate的“title”下的objectId为“5d8c8f4a4449a851f6kkk6o2”,有两个条件,第一个用于选择特定文档,第二个用于在嵌套数组中查找特定对象。收藏名称:toptenMongoose 具有两个条件的数组的嵌套对象元素的findOneAndUpdate,mongoose,Mongoose,我想将数组的嵌套对象元素的“displayTitle”更新为“12345”,其中topten=“under 50000”findOneAndUpdate的“title”下的objectId为“5d8c8f4a4449a851f6kkk6o2”,有两个条件,第一个用于选择特定文档,第二个用于在嵌套数组中查找特定对象。收藏名称:topten > db.topten.find().pretty(); { "_id" : ObjectId("5d8c8f254449a851f6aaa6r3
> db.topten.find().pretty();
{
"_id" : ObjectId("5d8c8f254449a851f6aaa6r3"),
"title" : "under 50000",
"products" : [
{
"_id" : ObjectId("5d8c8f4a4449a851f6kkk6o2"),
"productId" : "5cfe541ae759d9699b5213542",
"displayTitle" : "dhtyks",
"score" : 100,
"priority" : 50,
"description" : "Upcoming"
},
{
"_id" : ObjectId("5d8c8f8f4449a86544646"),
"productId" : "5cfe512bca044365efsdfgg45",
"displayTitle" : "Spice",
"score" : 10,
"priority" : 800,
"description" : "Advanced"
}
],
"created" : ISODate("2019-09-26T10:12:53.804Z"),
"updated" : ISODate("2019-10-03T05:24:19.296Z"),
"slug" : "under-10000",
"__v" : 0
{
"_id" : ObjectId("5d8c8f254449a851f6ddd9a1"),
"title" : "under 10000",
"products" : [
{
"_id" : ObjectId("5d8c8f4a4449a851f6ddd9a2"),
"productId" : "5cfe541ae759d9699b56759b",
"displayTitle" : "ABCDEF6",
"score" : 10,
"priority" : 200,
"description" : "New Gen"
},
{
"_id" : ObjectId("5d8c8f8f4449a851f6ddd9a4"),
"productId" : "5cfe512bca044365ef1dcd0f",
"displayTitle" : "Micromax",
"score" : 1,
"priority" : 200,
"description" : "Upcoming"
}
],
"created" : ISODate("2019-09-26T10:12:53.804Z"),
"updated" : ISODate("2019-10-03T05:24:19.296Z"),
"slug" : "under-10000",
"__v" : 0
我使用的代码是:
topten.findOneAndUpdate({"title" : "under 50000", "products": { $elemMatch: { "_id": "5d8c8f4a4449a851f6kkk6o2" } }},
{"products.0.displayTitle":"12345"},
function (err, doc) {
if (err) {
console.log(err);
reject(new Error(err));
return;
} else {
console.log('AAAAAAAAAAAAAAAAAAAAAAAA', doc)
res.status(200)
.json({
success: true,
msg: 'Attached product updated succesfully`);'
})
}
}
)
查询正在运行,但未更新my Db中的值。请帮助,我被困在这里。尝试使用$set分配产品。0.displayTitle如下:
topten.findOneAndUpdate(
{"title" : "under 50000",
"products": { $elemMatch: { "_id": "5d8c8f4a4449a851f6kkk6o2" } }},
{$set: { products.0.displayTitle: "12345"}},
function (err, doc) {
if (err) {
console.log(err);
reject(new Error(err));
return;
} else {
console.log('AAAAAAAAAAAAAAAAAAAAAAAA', doc)
res.status(200)
.json({
success: true,
msg: 'Attached product updated succesfully`);'
})
}
}
)
尝试在分配产品的位置使用$set.0.displayTitle,如下所示:
topten.findOneAndUpdate(
{"title" : "under 50000",
"products": { $elemMatch: { "_id": "5d8c8f4a4449a851f6kkk6o2" } }},
{$set: { products.0.displayTitle: "12345"}},
function (err, doc) {
if (err) {
console.log(err);
reject(new Error(err));
return;
} else {
console.log('AAAAAAAAAAAAAAAAAAAAAAAA', doc)
res.status(200)
.json({
success: true,
msg: 'Attached product updated succesfully`);'
})
}
}
)
谢谢你的建议@Michael Gervasoni{$set:{products.0.displaytTitle:“12345”},其他的一切都是一样的,而且很有效。再次感谢。感谢您的建议@Michael Gervasoni{$set:{products.0.displayTitle:“12345”},其他一切都是一样的,而且很有效。再次感谢。