codeDragon
codeDragon

Reputation: 565

How to $unset the whole object instead of leaving it empty or null in MongoDB

I have the following update query, which takes away the content of the object, however it leaves empty. I want to get rid of the whole object {}.

How can I do that? I have also tried, specifying the value for the properties instead of the booleans, which gives me the same result.

If I do this { $unset: { "courses.1.teachers.1": 1 }, that gives me null instead of {}. So none of them is optimal.

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)
    })
}

This is the result which leaves an empty object:

{
    "_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"
                }
            ]
        }
    ]
}

What I want is this:

{
    "_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"
                }
            ]
        }
    ]
}

Upvotes: 1

Views: 1209

Answers (1)

wscourge
wscourge

Reputation: 11291

According to the $unset docs, this is the desired behaviour:

When used with $ to match an array element, $unset replaces the matching element with null rather than removing the matching element from the array. This behavior keeps consistent the array size and element positions.

So you are using the wrong tool for the job.

Right tools:

  1. $pop operator: removes first or last array element
  2. $pull operator: removes item based on condition

document for examples:

{
    "_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"
                }
            ]
        }
    ]
}

example 1: $pop:

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

example 2: $pull:

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

Upvotes: 3

Related Questions