Mongoose 从 findByIdAndUpdate 中移除数组(继承模式)
Mongoose removes arrays from findByIdAndUpdate (inherited schema)
我正在尝试使用 findByIdAndUpdate
更新 Mongoose 模型
为了问题的缘故,模型被称为ItemVariant
,它继承自Item
。
有效载荷数据的示例是:
var data = {
arrayField: [ 1, 3 ],
description: 'This is a description' }
}
如果我打电话给
ItemVariant.findByIdAndUpdate(objectId, data);
我可以看到描述已更新,但 arrayField 根本没有传递给 mongo - 实际上所有数组都被删除了数据对象。
我一直在想办法做到这一点,查看了数组的设置 $pushAll
,但似乎没有任何效果。
我在这里遗漏了什么吗?
模型架构被继承。 Mongoose 模型如下所示:
function ItemVariantSchema() {
var self = this;
Schema.apply(this, arguments);
self.add({
description: [String],
arrayField: [Number]
});
}
util.inherits(ItemVariantSchema, ItemSchema);
// the field that represents the sub-class discriminator
var schemaOptions = {
discriminatorKey: 'type'
};
// create ItemVariant schema
var itemVariantSchema = new ItemVariantSchema({}, schemaOptions);
// create ItemVariant model
Item.discriminator('Variant', itemVariantSchema);
// Export the ItemVariant schema
module.exports = ItemVariantSchema;
mongod --verbose 输出示例:
command: findAndModify { findandmodify: "itemvariants", query: { _id: ObjectId('5541fb680dc0e9223bea1ddb') }, new: 1, remove: 0, upsert: 0, update: { $set: { description: "EDIT: some description" } } } update: { $set: { description: "EDIT: some description" } } nscanned:1 nscannedObjects:1 nMatched:1 nModified:0 keyUpdates:0 numYields:0 locks(micros) w:104 reslen:358 0ms
如您所见,arrayField
已被删除
我也尝试过类似的方法:
var data = {
$set: { description: 'This is a description' },
$push: { arrayField: [ 1, 3 ] }
}
但是 $push 数组在到达 mongo 时似乎是空的。
另外,正如@chridam 所建议的,我也尝试过类似的方法:
var data = {
$set: {
description: 'some description'
},
$addToSet: {
arrayField: {
$each: [2, 3]
}
}
}
现在输出如下所示:
command: findAndModify { findandmodify: "itemvariants", query: { _id: ObjectId('5541fb680dc0e9223bea1ddb') }, new: 1, remove: 0, upsert: 0, update: { $set: { description: "EDIT: another description" }, $addToSet: { arrayField: {} } } } update: { $set: { description: "EDIT: another description" }, $addToSet: { arrayField: {} } } nscanned:1 nscannedObjects:1 nMatched:1 nModified:1 keyUpdates:0 numYields:0 locks(micros) w:118 reslen:366 0ms
您需要使用 $set
运算符:
var data = {
"arrayField": [ 1, 3 ],
"description": "This is a description"
};
ItemVariant.findByIdAndUpdate(objectId, { "$set": data });
-- 更新 --
尝试在 description
字段上结合使用 $set
更新运算符和 $addToSet
运算符,在 arrayField
数组字段上使用 $each
修饰符。 $each
修饰符允许 $addToSet
运算符将多个值添加到数组字段:
var data = {
"arrayField": [ 1, 3 ],
"description": "This is a description"
};
var update = {
"$set": {
"description": data.description
},
"$addToSet": {
"arrayField": {
"$each": data.arrayField
}
}
};
ItemVariant.findByIdAndUpdate(objectId, update, options, callback);
看来这里的罪魁祸首就是继承。
猫鼬模型需要来自判别器:
var ItemVariant = Item.discriminators['Variant'];
ItemVariant.findByIdAndUpdate(objectId, data);
这对我有用。
阵列破坏有点转移注意力。我的问题并没有描绘出整个画面。感谢@chridam
的帮助
我正在尝试使用 findByIdAndUpdate
为了问题的缘故,模型被称为ItemVariant
,它继承自Item
。
有效载荷数据的示例是:
var data = {
arrayField: [ 1, 3 ],
description: 'This is a description' }
}
如果我打电话给
ItemVariant.findByIdAndUpdate(objectId, data);
我可以看到描述已更新,但 arrayField 根本没有传递给 mongo - 实际上所有数组都被删除了数据对象。
我一直在想办法做到这一点,查看了数组的设置 $pushAll
,但似乎没有任何效果。
我在这里遗漏了什么吗?
模型架构被继承。 Mongoose 模型如下所示:
function ItemVariantSchema() {
var self = this;
Schema.apply(this, arguments);
self.add({
description: [String],
arrayField: [Number]
});
}
util.inherits(ItemVariantSchema, ItemSchema);
// the field that represents the sub-class discriminator
var schemaOptions = {
discriminatorKey: 'type'
};
// create ItemVariant schema
var itemVariantSchema = new ItemVariantSchema({}, schemaOptions);
// create ItemVariant model
Item.discriminator('Variant', itemVariantSchema);
// Export the ItemVariant schema
module.exports = ItemVariantSchema;
mongod --verbose 输出示例:
command: findAndModify { findandmodify: "itemvariants", query: { _id: ObjectId('5541fb680dc0e9223bea1ddb') }, new: 1, remove: 0, upsert: 0, update: { $set: { description: "EDIT: some description" } } } update: { $set: { description: "EDIT: some description" } } nscanned:1 nscannedObjects:1 nMatched:1 nModified:0 keyUpdates:0 numYields:0 locks(micros) w:104 reslen:358 0ms
如您所见,arrayField
已被删除
我也尝试过类似的方法:
var data = {
$set: { description: 'This is a description' },
$push: { arrayField: [ 1, 3 ] }
}
但是 $push 数组在到达 mongo 时似乎是空的。
另外,正如@chridam 所建议的,我也尝试过类似的方法:
var data = {
$set: {
description: 'some description'
},
$addToSet: {
arrayField: {
$each: [2, 3]
}
}
}
现在输出如下所示:
command: findAndModify { findandmodify: "itemvariants", query: { _id: ObjectId('5541fb680dc0e9223bea1ddb') }, new: 1, remove: 0, upsert: 0, update: { $set: { description: "EDIT: another description" }, $addToSet: { arrayField: {} } } } update: { $set: { description: "EDIT: another description" }, $addToSet: { arrayField: {} } } nscanned:1 nscannedObjects:1 nMatched:1 nModified:1 keyUpdates:0 numYields:0 locks(micros) w:118 reslen:366 0ms
您需要使用 $set
运算符:
var data = {
"arrayField": [ 1, 3 ],
"description": "This is a description"
};
ItemVariant.findByIdAndUpdate(objectId, { "$set": data });
-- 更新 --
尝试在 description
字段上结合使用 $set
更新运算符和 $addToSet
运算符,在 arrayField
数组字段上使用 $each
修饰符。 $each
修饰符允许 $addToSet
运算符将多个值添加到数组字段:
var data = {
"arrayField": [ 1, 3 ],
"description": "This is a description"
};
var update = {
"$set": {
"description": data.description
},
"$addToSet": {
"arrayField": {
"$each": data.arrayField
}
}
};
ItemVariant.findByIdAndUpdate(objectId, update, options, callback);
看来这里的罪魁祸首就是继承。
猫鼬模型需要来自判别器:
var ItemVariant = Item.discriminators['Variant'];
ItemVariant.findByIdAndUpdate(objectId, data);
这对我有用。 阵列破坏有点转移注意力。我的问题并没有描绘出整个画面。感谢@chridam
的帮助