首页 > 解决方案 > 使用猫鼬从不同文档ID中的数组中删除条目

问题描述

我有一个这样的项目架构

const projectSchema = new mongoose.Schema({
 name: {
type: String,
required: true,
minlength: 2,
maxlength: 50
},
box: { 
type: [String],
required: false,
minlength: 0,
maxlength: 255
},

isActive: Boolean
});

我需要从不同的文档中删除olibox的条目

例如:

如果我有 4 个不同的projectSchemaID,并且我想olibox从这 4 个 projectSchemas 中删除 2 个特定的 ID。我该怎么做呢?

对于一个文档,我这样做是为了删除一个数组元素..

project= await Project.findByIdAndUpdate(req.project._id,{$pull :  {'box': req.params.oliId}},{new :true});


console.log("In post"); 

我可以$in以某种方式使用它吗?

标签: node.jsmongodbmongoose

解决方案


试试看这是否有效。我希望它确实如此。

Project.updateMany(
  {
    _id: { $in: ['xxxxxxxxxxxxxxxxxxxxxxx', 'xxxxxxxxxxxxxxxxxxxxxxx'] }
  },
  { $pull: { box: { $in: [8025, 7323] } } }
);

推荐阅读