admin管理员组文章数量:1415684
I have mongo document and when a new order appear, I need to push it or update (increment the quantity) if an order of a part_id
already exist.
{
user_id: '13',
stock: [
{
part_id: 'P456',
quantity: 3
},
{
part_id: 'P905',
quantity: 8
}
]
}
I have a tried to use {upsert: true}
and $inc
but could not find the solution.
I have mongo document and when a new order appear, I need to push it or update (increment the quantity) if an order of a part_id
already exist.
{
user_id: '13',
stock: [
{
part_id: 'P456',
quantity: 3
},
{
part_id: 'P905',
quantity: 8
}
]
}
I have a tried to use {upsert: true}
and $inc
but could not find the solution.
1 Answer
Reset to default 6I have a tried to use {upsert: true} and $inc but could not find the solution
The upsert will not support in array of object, you can try 2 queries first find and second update,
Count documents using countDocuments
:
let user_id = "13";
let part_id = "P456";
let hasDoc = await YourSchema.countDocuments({ user_id: user_id, "stock.part_id": part_id });
- Check condition if document exists then increment quantity by one
- Else push object in stock
// Document already exists, so increment it value
if (hasDoc > 0) {
await YourSchema.updateOne(
{ user_id: user_id, "stock.part_id": part_id },
{ $inc: { "stock.$.quantity": 1 } }
);
}
// Document not exists then add document
else {
await YourSchema.updateOne(
{ user_id: user_id },
{ $push: { stock: { part_id: part_id, quantity: 1 } } }
);
}
Second Option: You can update with aggregation pipeline starting from MongoDB 4.2,
$cond
to check ifpart_id
is in stock- yes,
$map
to iterate loop ofstock
and check condition ifpart_id
match then add 1 inquantity
otherwise return current object - no, add new object with
part_id
and quantity instock
using$concatArrays
- yes,
let user_id = "13";
let part_id = "P456";
db.collection.update(
{ user_id: user_id },
[{
$set: {
stock: {
$cond: [
{ $in: [part_id, "$stock.part_id"] },
{
$map: {
input: "$stock",
in: {
$cond: [
{ $eq: ["$$this.part_id", part_id] },
{
part_id: "$$this.part_id",
quantity: { $add: ["$$this.quantity", 1] }
},
"$$this"
]
}
}
},
{ $concatArrays: ["$stock", [{ part_id: part_id, quantity: 1 }]] }
]
}
}
}]
)
Playground
本文标签:
版权声明:本文标题:javascript - Push a document if not exist, update if exist in a nested array with mongoose - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745240119a2649276.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论