Mongoose splice子文档数组不保存

时间:2022-05-26 21:27:04

I have the following subdocument array called images which itself is contained within an array variants

我有以下子文档数组称为图像,它本身包含在数组变体中

  "_id" : 1
  "variants" : [ 
    {
        "code" : "ajs982-ask19",
        "images" : [ 
            "image1.jpg", 
            "image2.jpg", 
            "image3.jpg"
        ]
    }

I'm trying to remove one of the images from the array. I'm using Mongoose but so far I've been unable to remove it.

我正在尝试从阵列中删除其中一个图像。我正在使用Mongoose,但到目前为止我一直无法删除它。

Assuming that my Schema is called MySchema, I do the following

假设我的Schema名为MySchema,我会执行以下操作

MySchema.findOne({_id:1}, function(err, mySchema){
 myschema.variants[0].images.splice(1, 1);
 myschema.save();
});

If I log myschema to the console, I see that the array item at the index position 1 is removed but it is not updated in the database. I'd be grateful for any suggestions or recommendations as to how I might remove the item from the array.

如果我将myschema记录到控制台,我会看到索引位置1处的数组项被删除但数据库中没有更新。对于如何从数组中删除项目的任何建议或建议,我将不胜感激。

2 个解决方案

#1


0  

You should use $pull to remove item from child array:

您应该使用$ pull从子数组中删除项目:

MySchema.update( {_id: 1}, { $pull: {"variants.0.images": {$in: ["image1.jpg"] } } } )

MySchema.update({_ id:1},{$ pull:{“variants.0.images”:{$ in:[“image1.jpg”]}}}})

#2


0  

You need to explicitly tell mongoose that your object has changed.

This is a common gotcha when using Mongoose.

这是使用Mongoose时的常见问题。

Why don't my changes to arrays get saved when I update an element directly?

当我直接更新元素时​​,为什么不保存对数组的更改?

doc.array[3] = 'changed';

doc.array [3] ='改变';

doc.save();

doc.save();

A. Mongoose doesn't create getters/setters for array indexes; without them mongoose never gets notified of the change and so doesn't know to persist the new value. The work-around is to use MongooseArray#set available in Mongoose >= 3.2.0.

A. Mongoose不为数组索引创建getter / setter;没有他们mongoose永远不会得到变化的通知,所以不知道坚持新的价值。解决方法是使用Mongoose> = 3.2.0中提供的MongooseArray #set。

doc.array.set(3, 'changed');

doc.array.set(3,'改变');

doc.save();

doc.save();

// if running a version less than 3.2.0, you must mark the array modified before saving.

//如果运行的版本低于3.2.0,则必须在保存之前标记已修改的数组。

doc.array[3] = 'changed';

doc.array [3] ='改变';

doc.markModified('array');

doc.markModified( '阵列');

doc.save();

doc.save();

The markModified API is still available to use so this should work:

markModified API仍然可以使用,所以这应该工作:

MySchema.findOne({_id:1}, function(err, doc){
 doc.variants[0].images.splice(1, 1);
 doc.markModified('variants');
 doc.save();
});

Also, if you haven't already done so, some error handling to check where a document came back from your search would be ideal.

此外,如果您还没有这样做,那么检查文档从搜索中返回的位置的一些错误处理将是理想的。

#1


0  

You should use $pull to remove item from child array:

您应该使用$ pull从子数组中删除项目:

MySchema.update( {_id: 1}, { $pull: {"variants.0.images": {$in: ["image1.jpg"] } } } )

MySchema.update({_ id:1},{$ pull:{“variants.0.images”:{$ in:[“image1.jpg”]}}}})

#2


0  

You need to explicitly tell mongoose that your object has changed.

This is a common gotcha when using Mongoose.

这是使用Mongoose时的常见问题。

Why don't my changes to arrays get saved when I update an element directly?

当我直接更新元素时​​,为什么不保存对数组的更改?

doc.array[3] = 'changed';

doc.array [3] ='改变';

doc.save();

doc.save();

A. Mongoose doesn't create getters/setters for array indexes; without them mongoose never gets notified of the change and so doesn't know to persist the new value. The work-around is to use MongooseArray#set available in Mongoose >= 3.2.0.

A. Mongoose不为数组索引创建getter / setter;没有他们mongoose永远不会得到变化的通知,所以不知道坚持新的价值。解决方法是使用Mongoose> = 3.2.0中提供的MongooseArray #set。

doc.array.set(3, 'changed');

doc.array.set(3,'改变');

doc.save();

doc.save();

// if running a version less than 3.2.0, you must mark the array modified before saving.

//如果运行的版本低于3.2.0,则必须在保存之前标记已修改的数组。

doc.array[3] = 'changed';

doc.array [3] ='改变';

doc.markModified('array');

doc.markModified( '阵列');

doc.save();

doc.save();

The markModified API is still available to use so this should work:

markModified API仍然可以使用,所以这应该工作:

MySchema.findOne({_id:1}, function(err, doc){
 doc.variants[0].images.splice(1, 1);
 doc.markModified('variants');
 doc.save();
});

Also, if you haven't already done so, some error handling to check where a document came back from your search would be ideal.

此外,如果您还没有这样做,那么检查文档从搜索中返回的位置的一些错误处理将是理想的。