如何在MongoDB中取消设置整个对象,而不是将其留空或为空

时间:2017-11-07 13:21:15

标签: node.js mongodb

我有以下update query,它会删除object的内容,但会离开empty。我想摆脱整个对象{}

我该怎么做?我也尝试过为value而不是properties指定booleans,这给了我result

如果我这样做{ $unset: { "courses.1.teachers.1": 1 },那就会给我null而不是{}。所以它们都不是最佳的。

student.updateStudent = (fcallback) => {
    var studentObjectId = global.mongoId("5a019896f89c24926108e2bf")
    global.db.collection('students').updateOne({ "_id": studentObjectId }, { $unset: { "courses.1.teachers.1.firstName": 1, "courses.1.teachers.1.lastName": 1 } }, (err, result) => {
        if (err) {
            var jError = { "status": "error", "message": "ERROR -> user.js -> 001" }
            console.log(jError)
            return fcallback(true, jError)
        }
        var jOk = { "status": "ok", "message": "user.js -> deleted -> 000" }
        console.log(jOk)
        console.log(JSON.stringify(result))
        return fcallback(false, jOk)
    })
}

这是留下空对象的结果:

{
    "_id" : ObjectId("5a019896f89c24926108e2bf"),
    "firstName" : "Sarah",
    "lastName" : "Jepsen",
    "age" : 27,
    "courses" : [ 
        {
            "courseName" : "Web-development",
            "teachers" : [ 
                {
                    "firstName" : "Santiago",
                    "lastName" : "Donoso"
                }
            ]
        }, 
        {
            "courseName" : "Databases",
            "teachers" : [ 
                {
                    "firstName" : "Dany",
                    "lastName" : "Kallas"
                }, 
                {} //see here
            ]
        }, 
        {
            "teachers" : [ 
                {
                    "firstName" : "Roxana",
                    "lastName" : "Stolniceanu"
                }
            ]
        }
    ]
}

我想要的是:

{
    "_id" : ObjectId("5a019896f89c24926108e2bf"),
    "firstName" : "Sarah",
    "lastName" : "Jepsen",
    "age" : 27,
    "courses" : [ 
        {
            "courseName" : "Web-development",
            "teachers" : [ 
                {
                    "firstName" : "Santiago",
                    "lastName" : "Donoso"
                }
            ]
        }, 
        {
            "courseName" : "Databases",
            "teachers" : [ 
                {
                    "firstName" : "Dany",
                    "lastName" : "Kallas"
                }
            ]
        }, 
        {
            "teachers" : [ 
                {
                    "firstName" : "Roxana",
                    "lastName" : "Stolniceanu"
                }
            ]
        }
    ]
}

1 个答案:

答案 0 :(得分:2)

根据$unset docs,这是理想的行为:

  

当与$一起使用以匹配数组元素时,$ unset将匹配元素替换为null,而不是从数组中删除匹配元素。此行为使数组大小和元素位置保持一致。

所以你使用错误的工具来完成工作。

正确的工具:

  1. $pop operator:删除第一个或最后一个数组元素
  2. $pull运算符:根据条件
  3. 删除项目

    文件示例:

    {
        "_id" : ObjectId("5a019896f89c24926108e2bf"),
        "firstName" : "Sarah",
        "lastName" : "Jepsen",
        "age" : 27,
        "courses" : [ 
            {
                "courseName" : "Web-development",
                "teachers" : [ 
                    {
                        "firstName" : "Santiago",
                        "lastName" : "Donoso"
                    }
                ]
            }, 
            {
                "courseName" : "Databases",
                "teachers" : [ 
                    {
                        "firstName" : "Dany",
                        "lastName" : "Kallas"
                    }, 
                    {
                        "firstName": "Code",
                        "lastName": "Dragon"
                    }
                ]
            }, 
            {
                "teachers" : [ 
                    {
                        "firstName" : "Roxana",
                        "lastName" : "Stolniceanu"
                    }
                ]
            }
        ]
    }
    

    示例1:$pop

    global.db
        .collection('students')
        .updateOne(
            { _id : studentObjectId },
            { $pop: { "courses.1.teachers": 1 } } // 1 for last item, -1 for the first
        )
    

    示例2:$pull

    global.db
        .collection('students')
        .updateOne(
            { _id  : studentObjectId },
            { $pull: { "courses.1.teachers": { firstName: 'Code' } } }
        )