【问题标题】:Mongoose - Remove several objects from an array (not exact match)Mongoose - 从数组中删除多个对象(不完全匹配)
【发布时间】:2017-04-12 10:19:49
【问题描述】:

我有一个集合播放列表,其中包含一组项目

{

    userId: {
        type        : String,
        required    : true,
        index       : true,
        unique      : true
    },

    items: [
        {
            id: { // do not mix up with _id, which is the autogenerated id of the pair {id,type}. ID is itemId
                type        : Schema.Types.ObjectId
            },
            type: {
                type        : String
            }
        }
    ]

}

当我将一对 {id,type} 推送到 items 时,Mongo 会自动将 _id 字段添加到项目中(但我不在乎)。

现在我想从 items 数组中一次删除几个“对”。

我曾尝试使用 $pullAll,但它需要完全匹配,而且我不知道 _id,因此它不会从 items

中删除任何内容
playlistModel.update({userId:userId},{$pullAll:{items:[{id:"123",type:"video"},{id:"456",type:"video"}]}},null,function(err){

我尝试过使用具有不同变体的 $pull,但它从 items

中删除了所有对象
playlistModel.update({userId:userId},{$pull:{items:{"items.id":{$in:["123","456"]}}}},null,function(err){

playlistModel.update({userId:userId},{$pull:{items:{$in:[{id:"123",type:"video"},{id:"456",type:"video"}]}}},null,function(err){

我是遗漏了什么还是我在问一些没有实现的东西?

如果是后者,有没有办法解决这个_id问题?

【问题讨论】:

  • 没有清楚地理解你在这里想要做什么。您想删除匹配的 several pairs 而不是 all the pairs。这甚至意味着什么?
  • 假设我有一个播放列表文档,其“items”数组为 [{id:"123",type:"video"},{id:"456",type:"video"}, {id:"789",type:"video"}] (注意 mongodb 将 _id 添加到项目中的这 3 个对象)我想例如删除 {id:"123",type:"video"} 和 {id: "456",type:"video"} 来自 1 个查询中的 "items"(只留下其他对象,在此示例中为 {id:"789",type:"video"})我认为与 "exact" 混淆匹配”来自我不知道要删除的对象的 _id 的事实。希望现在有意义!
  • 为什么不禁用为嵌入文档创建_id 字段?看看这个stackoverflow.com/questions/17254008/…
  • 听起来不错!我会看看那个:)

标签: arrays node.js mongodb mongoose


【解决方案1】:

好的,我找到了一种使用 $pull 的方法:

playlistModel.update({userId:userId},{$pull:{items:{id:{$in:["123","456"]}}}},null,function(err){ 

它没有考虑类型,但我看不出有任何问题,因为 id 在所有类型中都是唯一的。

虽然我会稍等片刻,看看是否有人提供更好的解决方案

编辑

在 Veeram 的帮助下,我得到了另一个解决方案,IMO 更优雅,因为我在数据库中没有我不需要的 _id,并且 $pullAll 选项在这里似乎更正确

var playlistItemSchema = mongoose.Schema({
    id: { // do not mix up with autogenerated _id. id is itemId
        type        : Schema.Types.ObjectId
    },
    type: {
        type        : String
    }
},{ _id : false });

var schema = new Schema({

    userId: {
        type        : String,
        required    : true,
        index       : true,
        unique      : true
    },

    items: [playlistItemSchema]

});


playlistModel.update({userId:userId},{$pullAll:{items:[{id:"123",type:"video"},{id:"456",type:"video"}]}},null,function(err){

【讨论】:

  • 它应该可以正常工作。如果你愿意,你也可以在查询中添加type,但我认为这不是问题。
【解决方案2】:

提示: 您可以使用 _id 字段来处理您的 playlistModel 数据。

mongoose api : new mongoose.Types.ObjectId 生成 Object_id

let _id=new mongoose.Types.ObjectId;
     playlistModel.updateMany({_id:_id},{ $set: { name: 'bob' }}).exec(data=>{console.log('exec OK')});

【讨论】:

  • 欣赏提示,但它对这个问题没有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-19
  • 1970-01-01
  • 2022-01-23
  • 2019-10-23
  • 1970-01-01
  • 2016-03-16
相关资源
最近更新 更多