英文:
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
}
]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论