批量更新内部数组使用 MongoDB 和 Node.js

huangapple go评论101阅读模式
英文:

bulk updating in inner array with mongodb and node.js

问题

以下是翻译好的部分:

这是我的文档:

{
    "_id": 7,
    "sections": [
        {
            "sectionData": {
                "media": [
                    {
                        "_id": {
                            "$oid": "64a691e0c91b64aa2c0446ce"
                        },
                        "image": "1.jpg"
                    },
                    {
                        "_id": {
                            "$oid": "64a6922ac91b64aa2c0446d1"
                        },
                        "image": "1.jpg"
                    },
                    {
                        "_id": {
                            "$oid": "64a69240c91b64aa2c0446d2"
                        },
                        "image": "1.jpg"
                    },
                    {
                        "_id": {
                            "$oid": "64a6927dc91b64aa2c0446d3"
                        },
                        "image": "1.jpg"
                    }
                ]
            },
            "_id": {
                "$oid": "648847b944e80a387f627028"
            }
        },
        {
            "sectionData": {
                "media": []
            },
            "_id": {
                "$oid": "648aa5442b511184130685c7"
            }
        }
    ]
}

我想要通过匹配对象中的 _id 来更新媒体数组中的字段。

我将会收到一个包含如下信息的 id 和值的数组:

let ids = [
  { _id: "64a691e0c91b64aa2c0446ce", eligible: false },
  { _id: "64a6922ac91b64aa2c0446d1", eligible: false },
  { _id: "64a69240c91b64aa2c0446d2", eligible: true },
];

我想要匹配对象中的 _id 并分别更新 eligible 的值。

这是我的代码:

const albumId = 7;
const sectionId = "648847b944e80a387f627028";
let ids = [
  { _id: "64a691e0c91b64aa2c0446ce", eligible: false },
  { _id: "64a6922ac91b64aa2c0446d1", eligible: false },
  { _id: "64a69240c91b64aa2c0446d2", eligible: true },
];
await uploadAlbumSchema.updateMany(
  { _id: albumId, "sections._id": sectionId, "sections.sectionData.media._id": { $in: ids.map(obj => mongoose.Types.ObjectId(obj._id)) } },
  { $set: { "sections.$[outer].sectionData.media.$[inner].eligible": ids.map(obj => obj.eligible) } },
  { arrayFilters: [{ "outer._id": sectionId }, { "inner._id": { $in: ids.map(obj => mongoose.Types.ObjectId(obj._id)) } }] }
)

使用这段代码,它不按预期工作,它将 eligible 字段更新为数组。所以我只需要像这样更新相应字段的值:

{
    "_id": {
        "$oid": "64a691e0c91b64aa2c0446ce"
    },
    "image": "1.jpg",
    "eligible": false
}

希望这有所帮助。

英文:

The below given is my document

{
"_id": 7,
"sections": [
{
"sectionData": {
"media": [
{
"_id": {
"$oid": "64a691e0c91b64aa2c0446ce"
},
"image": "1.jpg"
},
{
"_id": {
"$oid": "64a6922ac91b64aa2c0446d1"
},
"image": "1.jpg"
},
{
"_id": {
"$oid": "64a69240c91b64aa2c0446d2"
},
"image": "1.jpg"
},
{
"_id": {
"$oid": "64a6927dc91b64aa2c0446d3"
},
"image": "1.jpg"
}
]
},
"_id": {
"$oid": "648847b944e80a387f627028"
}
},
{
"sectionData": {
"media": []
},
"_id": {
"$oid": "648aa5442b511184130685c7"
}
}
]
}

I wanted to update a field inside media array by matching _id in the objects.

i will recieve a array of ids and values like this

let ids = [
{ _id: "64a691e0c91b64aa2c0446ce", eligible: false },
{ _id: "64a6922ac91b64aa2c0446d1", eligible: false },
{ _id: "64a69240c91b64aa2c0446d2", eligible: true },
];

I wanted to match the _id in the object and update the eligible value respectively.

here is my code,

   const albumId = 7;
const sectionId = "648847b944e80a387f627028";
let ids = [
{ _id: "64a691e0c91b64aa2c0446ce", eligible: false },
{ _id: "64a6922ac91b64aa2c0446d1", eligible: false },
{ _id: "64a69240c91b64aa2c0446d2", eligible: true },
];
await uploadAlbumSchema.updateMany(
{ _id: albumId, "sections._id": sectionId, "sections.sectionData.media._id": { $in: ids.map(obj => mongoose.Types.ObjectId(obj._id)) } },
{ $set: { "sections.$[outer].sectionData.media.$[inner].eligible": ids.map(obj => obj.eligible)  } },
{ arrayFilters: [{ "outer._id": sectionId }, { "inner._id": { $in: ids.map(obj => mongoose.Types.ObjectId(obj._id)) } }] }
)

using this code its not working as expected its updating the eligible field as a array. so I need only to update the corresponding field with the respective value like this.

 {
"_id": {
"$oid": "64a691e0c91b64aa2c0446ce"
},
"image": "1.jpg",
"eligible":false
}

Thanks in advance

答案1

得分: 1

使用 db.collection.bulkWrite() 来更新多个具有不同文档的文档。

bulkWrite() 接受一个写入操作的数组,并执行每个操作。

例如("mongoose": "^7.3.1"

const albumId = '64a78b1e1a6e122b5b3ab0f7';
const sectionId = '64a78b1e1a6e122b5b3ab0f8';
const ids = [
  { _id: '64a78b1e1a6e122b5b3ab0f9', eligible: false },
  { _id: '64a78b1e1a6e122b5b3ab0fa', eligible: false },
  { _id: '64a78b1e1a6e122b5b3ab0fb', eligible: true },
];
const r = await UploadAlbum.bulkWrite(
  ids.map((item) => {
    return {
      updateOne: {
        filter: {
          _id: new mongoose.Types.ObjectId(albumId),
          'sections._id': new mongoose.Types.ObjectId(sectionId),
        },
        update: { $set: { 'sections.$[].sectionData.media.$[m].eligible': item.eligible } },
        arrayFilters: [{ 'm._id': new mongoose.Types.ObjectId(item._id) }],
      },
    };
  }),
  { ordered: false },
);
console.log(r)

调试日志:

BulkWriteResult {
  insertedCount: 0,
  matchedCount: 3,
  modifiedCount: 3,
  deletedCount: 0,
  upsertedCount: 0,
  upsertedIds: {},
  insertedIds: {}
}

输入文档:

[
  {
    _id: ObjectId("64a78b1e1a6e122b5b3ab0f7"),
    sections: [
      {
        sectionData: {
          media: [
            {
              image: '1.jpg',
              _id: ObjectId("64a78b1e1a6e122b5b3ab0f9")
            },
            {
              image: '1.jpg',
              _id: ObjectId("64a78b1e1a6e122b5b3ab0fa")
            },
            {
              image: '1.jpg',
              _id: ObjectId("64a78b1e1a6e122b5b3ab0fb")
            },
            {
              image: '1.jpg',
              _id: ObjectId("64a78b1e1a6e122b5b3ab0fc")
            }
          ]
        },
        _id: ObjectId("64a78b1e1a6e122b5b3ab0f8")
      },
      {
        sectionData: {
          media: [
            {
              image: 'a.jpg',
              _id: ObjectId("64a78b1e1a6e122b5b3ab0fe")
            },
            {
              image: 'a.jpg',
              _id: ObjectId("64a78b1e1a6e122b5b3ab0ff")
            },
            {
              image: 'a.jpg',
              _id: ObjectId("64a78b1e1a6e122b5b3ab100")
            },
            {
              image: 'a.jpg',
              _id: ObjectId("64a78b1e1a6e122b5b3ab101")
            }
          ]
        },
        _id: ObjectId("64a78b1e1a6e122b5b3ab0fd")
      }
    ],
    __v: 0
  }
]

输出文档:

[
  {
    _id: ObjectId("64a78b1e1a6e122b5b3ab0f7"),
    sections: [
      {
        sectionData: {
          media: [
            {
              image: '1.jpg',
              _id: ObjectId("64a78b1e1a6e122b5b3ab0f9"),
              eligible: false
            },
            {
              image: '1.jpg',
              _id: ObjectId("64a78b1e1a6e122b5b3ab0fa"),
              eligible: false
            },
            {
              image: '1.jpg',
              _id: ObjectId("64a78b1e1a6e122b5b3ab0fb"),
              eligible: true
            },
            {
              image: '1.jpg',
              _id: ObjectId("64a78b1e1a6e122b5b3ab0fc")
            }
          ]
        },
        _id: ObjectId("64a78b1e1a6e122b5b3ab0f8")
      },
      {
        sectionData: {
          media: [
            {
              image: 'a.jpg',
              _id: ObjectId("64a78b1e1a6e122b5b3ab0fe")
            },
            {
              image: 'a.jpg',
              _id: ObjectId("64a78b1e1a6e122b5b3ab0ff")
            },
            {
              image: 'a.jpg',
              _id: ObjectId("64a78b1e1a6e122b5b3ab100")
            },
            {
              image: 'a.jpg',
              _id: ObjectId("64a78b1e1a6e122b5b3ab101")
            }
          ]
        },
        _id: ObjectId("64a78b1e1a6e122b5b3ab0fd")
      }
    ],
    __v: 0
  }
]
英文:

Use db.collection.bulkWrite() to update multiple documents with different documents.

> bulkWrite() takes an array of write operations and executes each of them.

E.g.("mongoose": "^7.3.1")

const albumId = '64a78b1e1a6e122b5b3ab0f7';
const sectionId = '64a78b1e1a6e122b5b3ab0f8';
const ids = [
  { _id: '64a78b1e1a6e122b5b3ab0f9', eligible: false },
  { _id: '64a78b1e1a6e122b5b3ab0fa', eligible: false },
  { _id: '64a78b1e1a6e122b5b3ab0fb', eligible: true },
];
const r = await UploadAlbum.bulkWrite(
  ids.map((item) => {
    return {
      updateOne: {
        filter: {
          _id: new mongoose.Types.ObjectId(albumId),
          'sections._id': new mongoose.Types.ObjectId(sectionId),
        },
        update: { $set: { 'sections.$[].sectionData.media.$[m].eligible': item.eligible } },
        arrayFilters: [{ 'm._id': new mongoose.Types.ObjectId(item._id) }],
      },
    };
  }),
  { ordered: false },
);
console.log(r)

Debug logs:

BulkWriteResult {
  insertedCount: 0,
  matchedCount: 3,
  modifiedCount: 3,
  deletedCount: 0,
  upsertedCount: 0,
  upsertedIds: {},
  insertedIds: {}
}

Input documents:

[
  {
    _id: ObjectId("64a78b1e1a6e122b5b3ab0f7"),
    sections: [
      {
        sectionData: {
          media: [
            {
              image: '1.jpg',
              _id: ObjectId("64a78b1e1a6e122b5b3ab0f9")
            },
            {
              image: '1.jpg',
              _id: ObjectId("64a78b1e1a6e122b5b3ab0fa")
            },
            {
              image: '1.jpg',
              _id: ObjectId("64a78b1e1a6e122b5b3ab0fb")
            },
            {
              image: '1.jpg',
              _id: ObjectId("64a78b1e1a6e122b5b3ab0fc")
            }
          ]
        },
        _id: ObjectId("64a78b1e1a6e122b5b3ab0f8")
      },
      {
        sectionData: {
          media: [
            {
              image: 'a.jpg',
              _id: ObjectId("64a78b1e1a6e122b5b3ab0fe")
            },
            {
              image: 'a.jpg',
              _id: ObjectId("64a78b1e1a6e122b5b3ab0ff")
            },
            {
              image: 'a.jpg',
              _id: ObjectId("64a78b1e1a6e122b5b3ab100")
            },
            {
              image: 'a.jpg',
              _id: ObjectId("64a78b1e1a6e122b5b3ab101")
            }
          ]
        },
        _id: ObjectId("64a78b1e1a6e122b5b3ab0fd")
      }
    ],
    __v: 0
  }
]

Output documents:

[
  {
    _id: ObjectId("64a78b1e1a6e122b5b3ab0f7"),
    sections: [
      {
        sectionData: {
          media: [
            {
              image: '1.jpg',
              _id: ObjectId("64a78b1e1a6e122b5b3ab0f9"),
              eligible: false
            },
            {
              image: '1.jpg',
              _id: ObjectId("64a78b1e1a6e122b5b3ab0fa"),
              eligible: false
            },
            {
              image: '1.jpg',
              _id: ObjectId("64a78b1e1a6e122b5b3ab0fb"),
              eligible: true
            },
            {
              image: '1.jpg',
              _id: ObjectId("64a78b1e1a6e122b5b3ab0fc")
            }
          ]
        },
        _id: ObjectId("64a78b1e1a6e122b5b3ab0f8")
      },
      {
        sectionData: {
          media: [
            {
              image: 'a.jpg',
              _id: ObjectId("64a78b1e1a6e122b5b3ab0fe")
            },
            {
              image: 'a.jpg',
              _id: ObjectId("64a78b1e1a6e122b5b3ab0ff")
            },
            {
              image: 'a.jpg',
              _id: ObjectId("64a78b1e1a6e122b5b3ab100")
            },
            {
              image: 'a.jpg',
              _id: ObjectId("64a78b1e1a6e122b5b3ab101")
            }
          ]
        },
        _id: ObjectId("64a78b1e1a6e122b5b3ab0fd")
      }
    ],
    __v: 0
  }
]

huangapple
  • 本文由 发表于 2023年7月6日 20:43:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76628966.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定