【问题标题】:Remove by _id inside a nested array, inside of a collection在集合内的嵌套数组中按 _id 删除
【发布时间】:2019-09-20 15:15:12
【问题描述】:

这是我的 mongoDb 足球运动员收藏:

[
    {
        "_id" : ObjectId("5d83b4a7e5511f28847f1884"),
        "prenom" : "djalil",
        "pseudo" : "dja1000",
        "email" : "djalil@gmail.com",
        "selectionned" : [ 
            {
                "_id" : "5d83af3be5511f28847f187f",
                "role" : "footballeur",
                "prenom" : "Gilbert",
                "pseudo" : "Gilbert",
            }, 
            {
                "_id" : "5d83b3d5e5511f28847f1883",
                "role" : "footballeur",
                "prenom" : "Xavier",
                "pseudo" : "xav4544",
            }
        ]
    },

    {
        "_id" : ObjectId("5d83afa8e5511f28847f1880"),
        "prenom" : "Rolande",
        "pseudo" : "Rolande4000",
        "email" : "rolande@gmail.com",
        "selectionned" : [ 

            {
                "_id" : "5d83b3d5e5511f28847f1883",
                "role" : "footballeur",
                "prenom" : "Xavier",
                "pseudo" : "xav4544",
            }
        ]

    }

}

如何删除所有集合中具有 5d83b3d5e5511f28847f1883 _id 的每个选定人员?

我确实需要 xavier 从任何“选定”数组中消失,就像在 SQL 语言中执行“删除级联”一样

这是我没有运气的尝试:

   function delete_fb_from_all(fb){
        var ObjectId = require('mongodb').ObjectID; //working
        var idObj = ObjectId(fb._id); //working

    try {
            db.collection('footballers').remove( { "selectionned._id" : idObj } );
            console.log('All have been erased');    

        } catch (e) {
            console.log(e);
    }

}

这也不起作用:

db.collection('footballers.selectionned').remove( { "_id" : idObj } );

我真的不知道该怎么做。

我正在尝试这个:

db.collection.update({'footballers.selectionned':  idObj }, {$pull: {footballers:{ selectionned:  idObj}}})

这是错误:

TypeError: db.collection.update is not a function

我认为解决方案可能存在: https://docs.mongodb.com/manual/reference/operator/update/pull/#pull-array-of-documents

编辑 1

我目前正在尝试这个:

var ObjectId = require('mongodb').ObjectID; //working
        var idObj = ObjectId(fb._id); //working
        try {
            db.collection('footballers').update(
              { },
              { $pull: { selectionned: { _id: idObj } } },
              { multi: true }
            )
        } catch (e) {
            console.log(e);

        }

已解决:

指定电子邮件,它现在正在工作,我猜问题来自 _id 字段:

try {
    db.collection('footballers').update(
      { },
      { $pull: { selectionned: { email: fb.email } } },
      { multi: true }
    )
} catch (e) {
    console.log(e);

}

【问题讨论】:

    标签: mongodb express


    【解决方案1】:

    对象 ID:

    问题可能在于您的对象 ID 创建。无需使用 mongoDB 对象 id 制作 string-id。

        // No need
        var ObjectId = require('mongodb').ObjectID;
        var idObj = ObjectId(fb._id);
    
        // do as normal string
        db.collection('footballers').remove( { "selectionned._id" : fb._id } );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-09
      • 1970-01-01
      • 1970-01-01
      • 2020-11-26
      • 1970-01-01
      相关资源
      最近更新 更多