Bhuvan M
Bhuvan M

Reputation: 61

How to unset the objects from objects

This is my document:

{
    "_id" : ObjectId("5ccaa152feee5f2e60dff06f"),
    "name" : "ABC",
    "hobbies" : {
        "cricket" : {
            "hobbyName" : "cricket"
        },
        "football" : {
            "hobbyName" : "football"
        }
    }
},
{
    "_id" : ObjectId("5ccaa196feee5f2e60dff070"),
    "name" : "D",
    "hobbies" : {
        "Tennis" : {
            "hobbyName" : "Tennis"
        },
        "Volleyball" : {
            "hobbyName" : "Volleyball"
        },
        "basketball" : {
            "hobbyName" : "basketball"
        }
    }
}

var find = [ "Tennis", "football"];

Above my document under hobbies object suppose Tennis & football value is there , I want to unset that particular object

My code:

var find = [ "Tennis", "football"];
db.Hobbies.find({})
.forEach(function(doc){
    var _id = doc._id;
    for(let i=0;i<find.length;i++){ 
    let remove  = find[i];
    let concate = "hobbies." + remove;
    let text = '"'+concate+'"';
        db.Hobbies.update(
            {'_id': _id},
            {$unset: { text : {_id: remove} }} 
        )
    }
})

My expected output is:

 {
    "_id" : ObjectId("5ccaa152feee5f2e60dff06f"),
    "name" : "ABC",
    "hobbies" : {
        "cricket" : {
            "hobbyName" : "cricket"
        }
    }
},
{
    "_id" : ObjectId("5ccaa196feee5f2e60dff070"),
    "name" : "D",
    "hobbies" : {
        "Volleyball" : {
            "hobbyName" : "Volleyball"
        },
        "basketball" : {
            "hobbyName" : "basketball"
        }
    }
}

Upvotes: 2

Views: 90

Answers (2)

Tom Slabbaert
Tom Slabbaert

Reputation: 22296

Read a little bit about the $unset operator. Basically you need to specify the Field name to unset. Correct snippet should look like this:

db.Hobbies.update(
    {'_id': _id},
    {$unset: { "hobbies.Tennis": "", "hobbies.footbal": ""}, 
)

also if you want to remove all tennis and football hobbies:

db.Hobbies.update(
    {},
    {$unset: { "hobbies.Tennis": "", "hobbies.footbal": ""}, 
    {multi: true}
)

EDIT: a dynamic way.

let find = [ "Tennis", "football"];
db.Hobbies.find({})
    .forEach(function(doc){
       var _id = doc._id;
       let unset_obj = {};
       for(let i=0; i < find.length; i++){ 
            let key = `hobbies.${find[i]}`;
            unset_obj[key] = "";
        }
        db.Hobbies.update(
            {'_id': _id},
            {$unset: unset_obj} 
        )
    }
})

dynamic way all documents:

let find = [ "Tennis", "football"];
var _id = doc._id;
let unset_obj = {};
for(let i=0; i < find.length; i++){ 
    let key = `hobbies.${find[i]}`;
    unset_obj[key] = "";
}
db.Hobbies.update(
            {},
            {$unset: unset_obj},
            {multi: true}
        )
    }
})

Upvotes: 1

Shivani Tyagi
Shivani Tyagi

Reputation: 182

Do not loop on db.Hobbies.find({}). try direct:

    var find = [ "Tennis", "football"];
    for(let i=0;i<find.length;i++){ 
    let remove  = find[i];
    let concate = "hobbies." + remove;
    console.log(concate)
        db.Hobbies.update({},{$unset:{concate : 1}},{multi : true} 
        )
    }

this will unset all the documents and will also skip a loop and query

Upvotes: 0

Related Questions