[英]Mongo adding a field to a specific object in an array of objects using updateOne during a bulkUpdateOps
Use case用例
Adding a field to a specific object in an array of objects using updateOne during a bulkUpdateOps在 bulkUpdateOps 期间使用 updateOne 将字段添加到对象数组中的特定 object
Blockers阻滞剂
Data example数据示例
Our purchaseorders model looks like this:我们的采购订单 model 如下所示:
{
_id: uuid,
...rest,
documents:[
{
_id:uuid,
forignKey2:string (optional),
keyIWantToAdd:string (optional),
...rest
}
]
}
So if I have所以如果我有
{
_id:*1,
documents:[
{
_id:*2,
forignKey2:'no-test',
...rest
},
{
_id:*3,
forignKey2:'test',
...rest
},
]
}
I want to add a key and value like this (really I'm willing to do anything to set these values, this is just the closest I have been able to get):我想添加这样的键和值(真的,我愿意做任何事情来设置这些值,这只是我能得到的最接近的值):
var bulkUpdateOps = db.purchaseorders.initializeOrderedBulkOp();
bulkUpdateOps.find('*1').updateOne({
$set:{
documents.[index of document object with forignKey2:'test' or _id:*3 whichever is easier].keyIWantToAdd:'valueIWantToAdd'
}
})
bulkUpdateOps.execute();
Any help or suggestions would be greatly appreciated.任何帮助或建议将不胜感激。
@rickhg12hs posted exactly what I was looking for. @rickhg12hs 发布了我正在寻找的内容。 For anyone else using mongodb.com/docs/manual/reference/method/Bulk.find.arrayFilters the bulk find is being ran on an array of objects like this:
{ grades:[ { grade: 85, mean: number } ] }
对于使用 mongodb.com/docs/manual/reference/method/Bulk.find.arrayFilters 的其他任何人,批量查找正在这样的对象数组上运行:
{ grades:[ { grade: 85, mean: number } ] }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.