简体   繁体   English

如何在 mongoose 中使用不同的键更新多个数组元素

[英]How to update multiple array elements with different keys in mongoose

I have document containing lists.我有包含列表的文档。 For example, the users document looks like:例如,用户文档如下所示:

[
  {
    _id: 1,
    photos: [
      {_id: 1, url:'https://example.com/1.jpg', size: 100},
      {_id: 2, url:'https://example.com/2.jpg', size: 123}
    ]
  },
  {
    _id: 2,
    photos: [
      {_id: 1, url:'https://example.com/1.jpg', size: 100},
      {_id: 2, url:'https://example.com/2.jpg', size: 123}
    ]
  },
...
]

And I want to update this document with the following data:我想用以下数据更新这个文档:

updateUserData: {
  _id: 2,
  photos: [{_id:1, size:200}, {_id:2, size:300}]
}

And the result should be:结果应该是:

[
  {
    _id: 1,
    photos: [
      {_id: 1, url:'https://example.com/1.jpg', size: 100},
      {_id: 2, url:'https://example.com/2.jpg', size: 123}
    ]
  },
  {
    _id: 2,
    photos: [
      {_id: 1, url:'https://example.com/1.jpg', size: 200},
      {_id: 2, url:'https://example.com/2.jpg', size: 300}
    ]
  },
...
]

How can I get this done by using one command in mongoose?如何通过在 mongoose 中使用一个命令来完成此操作?

I have document containing lists.我有包含列表的文档。 For example, the users document looks like:例如,用户文档如下所示:

[
  {
    _id: 1,
    photos: [
      {_id: 1, url:'https://example.com/1.jpg', size: 100},
      {_id: 2, url:'https://example.com/2.jpg', size: 123}
    ]
  },
  {
    _id: 2,
    photos: [
      {_id: 1, url:'https://example.com/1.jpg', size: 100},
      {_id: 2, url:'https://example.com/2.jpg', size: 123}
    ]
  },
...
]

And I want to update this document with the following data:我想用以下数据更新这个文档:

updateUserData: {
  _id: 2,
  photos: [{_id:1, size:200}, {_id:2, size:300}]
}

And the result should be:结果应该是:

[
  {
    _id: 1,
    photos: [
      {_id: 1, url:'https://example.com/1.jpg', size: 100},
      {_id: 2, url:'https://example.com/2.jpg', size: 123}
    ]
  },
  {
    _id: 2,
    photos: [
      {_id: 1, url:'https://example.com/1.jpg', size: 200},
      {_id: 2, url:'https://example.com/2.jpg', size: 300}
    ]
  },
...
]

How can I get this done by using one command in mongoose?如何通过在 mongoose 中使用一个命令来完成此操作?

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

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