简体   繁体   English

使用 mongoose (MongoDB) 编辑数组中的多个对象

[英]Edit multiple objects in array using mongoose (MongoDB)

So I tried several ways, but I can't, I can modify several objects with the same key but I can't modify any with different keys, if anyone can help me is quite a complex problem所以我尝试了几种方法,但我不能,我可以用相同的键修改几个对象,但我不能用不同的键修改任何对象,如果有人能帮助我,这是一个非常复杂的问题

{ 
  id: 123, 
  "infos": [ 
    { name: 'Joe', value: 'Disabled', id: 0 }, 
    { name: 'Adam', value: 'Enabled', id: 0 }
  ]
};

In my database I have a collection with an array and several objects inside which gives this.在我的数据库中,我有一个集合,其中包含一个数组和几个对象,其中给出了这个。

I want to modify these objects, filter by their name and modify the value.我想修改这些对象,按名称过滤并修改值。

To give you a better example, my site returns me an object with the new data, and I want to modify the database object with the new object, without clearing the array, the name key never changes.给你一个更好的例子,我的网站返回给我一个 object 的新数据,我想用新的 object 修改数据库 object,而不清除数组,名称键永远不会改变。

const object = [
  { name: 'Joe', value: 'Hey', id: 1 }, 
  { name: 'Adam', value: 'None', id: 1 }
];

for(const obj in object) {
  Schema.findOneAndUpdate({ id: 123 }, {
    $set: {
      [`infos.${obj}.value`]: "Test"
    }
  })
}

This code works but it is not optimized, it makes several requests, I would like to do everything in one request, and also it doesn't update the id, only the value.此代码有效但未优化,它发出多个请求,我想在一个请求中完成所有操作,而且它不更新 ID,仅更新值。

If anyone can help me that would be great, I've looked everywhere and can't find anything如果有人能帮助我,那就太好了,我到处都找遍了,找不到任何东西

My schema structure我的模式结构

new Schema({
  id: { "type": String, "required": true, "unique": true }, 
  infos: []
})

I use the $addToSet method to insert objects into the infos array我使用$addToSet方法将对象插入到infos数组中

Try This:尝试这个:

db.collection.update({
  id: 123,
  
},
{
  $set: {
    "infos.$[x].value": "Value",
    "infos.$[x].name": "User"
  }
},
{
  arrayFilters: [
    {
      "x.id": {
        $in: [ 
          1 
        ]
      }
    },
    
  ],
  multi: true
})
  1. The all positional $[] operator acts as a placeholder for all elements in the array field.全位置$[]运算符充当数组字段中所有元素的占位符。

  2. In $in you can use dynamic array of id.$in你可以使用 id 的动态array Ex:前任:

    const ids = [1,2,..n] const ids = [1,2,..n]

    db.collection.update( //Same code as it is... { arrayFilters: [ { "x.id": { $in: ids } }, ], multi: true }) db.collection.update( //Same code as it is... { arrayFilters: [ { "x.id": { $in: ids } }, ], multi: true })

MongoPlayGround Link: https://mongoplayground.net/p/Tuz831lkPqk MongoPlayGround 链接: https://mongoplayground.net/p/Tuz831lkPqk

Maybe you look for something like this:也许您正在寻找这样的东西:

db.collection.update({},
{
  $set: {
   "infos.$[x].value": "test1",
   "infos.$[x].id": 10,
   "infos.$[y].value": "test2",
   "infos.$[y].id": 20
   }
 },
{
 arrayFilters: [
{
  "x.name": "Adam"
},
{
  "y.name": "Joe"
}
],
  multi: true
})

Explained:解释:

You define arrayFilters for all names in objects you have and update the values & id in all documents...您为您拥有的对象中的所有名称定义 arrayFilters 并更新所有文档中的值和 id ...

playground操场

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM