Mongodb 用不同的值更新多个文档
Mongodb update multiple documents with different values
我一直在尝试将 updatemany
与猫鼬一起使用。我想使用对象数组更新数据库中的值。
[
{
"variantId": "5e1760fbdfaf28038242d676",
"quantity": 5
},
{
"variantId": "5e17e67b73a34d53160c7252",
"quantity": 13
}
]
我想使用 variantId 作为过滤器。
模型架构是:
let variantSchema = new mongoose.Schema({
variantName: String,
stocks: {
type: Number,
min: 0
},
regularPrice: {
type: Number,
required: true
},
salePrice: {
type: Number,
required: true
}
})
我想使用 variantId 过滤模型,然后减少库存。
我不认为这可以通过单个 Model.updateMany
查询来完成。您将需要循环数组并改用 Model.update
。
for (const { variantId, quantity } of objects) {
Model.update({ _id: variantId }, { $inc: { stocks: -quantity } });
}
为了 运行 事务中的这个 (https://mongoosejs.com/docs/transactions.html),代码应该看起来像这样(但是我还没有尝试或测试过这个):
mongoose.startSession().then(async session => {
session.startTransaction();
for (const { variantId, quantity } of objects) {
await Model.update({ _id: variantId }, { $inc: { stocks: -quantity } }, { session });
}
await session.commitTransaction();
});
因为您需要使用多个条件更新多个文档,那么 .updateMany()
将不起作用 - 只有当您需要更新具有相同值的多个文档时它才会起作用,试试下面的查询,这将帮助您在一次数据库调用中完成:
const Mongoose = require("mongoose");
let variantSchema = new mongoose.Schema({
variantName: String,
stocks: {
type: Number,
min: 0
},
regularPrice: {
type: Number,
required: true
},
salePrice: {
type: Number,
required: true
}
})
const Variant = mongoose.model('variant', variantSchema, 'variant');
let input = [
{
"variantId": "5e1760fbdfaf28038242d676",
"quantity": 5
},
{
"variantId": "5e17e67b73a34d53160c7252",
"quantity": 13
}
]
let bulkArr = [];
for (const i of input) {
bulkArr.push({
updateOne: {
"filter": { "_id": Mongoose.Types.ObjectId(i.variantId) },
"update": { $inc: { "stocks": - i.quantity } }
}
})
}
Variant.bulkWrite(bulkArr)
我一直在尝试将 updatemany
与猫鼬一起使用。我想使用对象数组更新数据库中的值。
[
{
"variantId": "5e1760fbdfaf28038242d676",
"quantity": 5
},
{
"variantId": "5e17e67b73a34d53160c7252",
"quantity": 13
}
]
我想使用 variantId 作为过滤器。 模型架构是:
let variantSchema = new mongoose.Schema({
variantName: String,
stocks: {
type: Number,
min: 0
},
regularPrice: {
type: Number,
required: true
},
salePrice: {
type: Number,
required: true
}
})
我想使用 variantId 过滤模型,然后减少库存。
我不认为这可以通过单个 Model.updateMany
查询来完成。您将需要循环数组并改用 Model.update
。
for (const { variantId, quantity } of objects) {
Model.update({ _id: variantId }, { $inc: { stocks: -quantity } });
}
为了 运行 事务中的这个 (https://mongoosejs.com/docs/transactions.html),代码应该看起来像这样(但是我还没有尝试或测试过这个):
mongoose.startSession().then(async session => {
session.startTransaction();
for (const { variantId, quantity } of objects) {
await Model.update({ _id: variantId }, { $inc: { stocks: -quantity } }, { session });
}
await session.commitTransaction();
});
因为您需要使用多个条件更新多个文档,那么 .updateMany()
将不起作用 - 只有当您需要更新具有相同值的多个文档时它才会起作用,试试下面的查询,这将帮助您在一次数据库调用中完成:
const Mongoose = require("mongoose");
let variantSchema = new mongoose.Schema({
variantName: String,
stocks: {
type: Number,
min: 0
},
regularPrice: {
type: Number,
required: true
},
salePrice: {
type: Number,
required: true
}
})
const Variant = mongoose.model('variant', variantSchema, 'variant');
let input = [
{
"variantId": "5e1760fbdfaf28038242d676",
"quantity": 5
},
{
"variantId": "5e17e67b73a34d53160c7252",
"quantity": 13
}
]
let bulkArr = [];
for (const i of input) {
bulkArr.push({
updateOne: {
"filter": { "_id": Mongoose.Types.ObjectId(i.variantId) },
"update": { $inc: { "stocks": - i.quantity } }
}
})
}
Variant.bulkWrite(bulkArr)