简体   繁体   English

Mongo 在 bulkUpdateOps 期间使用 updateOne 将字段添加到对象数组中的特定 object

[英]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阻滞剂

  1. I have been unable to find a way to identify and update a specific object in the subdocument array of a specific record.我一直无法找到一种方法来识别和更新特定记录的子文档数组中的特定 object。
  2. I only have access to this DB through MongoDB Compass, and I plan to use the provided mongosh tool.我只能通过 MongoDB Compass 访问这个数据库,并且我打算使用提供的 mongosh 工具。

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.

相关问题 Mongo UpdateOne 用于使用 Lambda 和 FieldDefinition 将子文档添加到数组 C# - Mongo UpdateOne for adding a subdocument to array C# using Lambda and FieldDefinition 使用 updateOne 方法更新数组内的 object 字段 - 引发错误“无法在元素中创建字段 'url'” - Using updateOne method to update an object field inside array - throws error “Cannot create field 'url' in element” Mongo - 将 object 的特定字段区分为数组 - Mongo - distinct of specific field of an object into an array 如何使用 mongo 查询获取对象数组的特定对象? - How to get an object's array's specific objects with mongo query? UpdateOne 不使用 mongoDB 中的对象数组 - UpdateOne is not working with array of objects in mongoDB 使用 go mongo-driver 在对象数组中查找匹配字段 - Find matching field in an array of objects in an object with go mongo-driver 向 mongo 聚合中的对象数组中的每个 object 添加字段 - Add field to every object in array of objects in mongo aggregation 将 Mongo DB 集合字段从 object 更新为对象数组 - Updating Mongo DB collection field from object to array of objects Mongo db-使用嵌套字段(嵌套数组和对象)查询文档 - Mongo db - Querying documents using nested field (nested array and objects) Mongo如何获取具有值或对象值的特定数组字段 - Mongo how to get specific array field with value or object value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM