user550881
user550881

Reputation: 779

How to use $unset and $set in combination in mongoDB

I have record like this :

{
    "Date" : ISODate("2013-06-28T18:30:00Z"),
    "Details" : {
            "Amount1" : -200,
            "Amount2" : 2800,
            "Amount3" : -100
    },
    'NID' : 'T123RT',
    'PID' : 'P123RT',
    "SettAmount" : 2500,
    "SettStatus" : "completed",
    "Status" : "completed",
    "StoreID" : "51ea54279d867b040b000008",
    "_id" : ObjectId("51ea54279d867b040b000013")
}

I am trying to update the document like :

db.settlements.update({
    'StoreID' : "51ea54279d867b040b000008",
    'Date' : ISODate("2013-06-28T18:30:00Z")
}, {
    $unset : {
        'NID' : "",
        'PID' : ""
    }
    }, {
    $set : {
        'SettStatus' : 'start',
        'Status' : 'pending'
    }
});

But, only unset operation is successful. what is the error in above query........?

Upvotes: 35

Views: 30980

Answers (1)

roman
roman

Reputation: 117380

you have too many braces, here's correct command:

db.settlements.update(
    {
        'StoreID': "51ea54279d867b040b000008",
        'Date': ISODate("2013-06-28T18:30:00Z")
    }, 
    {
        $unset: {
            'NID' : "",
            'PID' : ""
        }, 
        $set: {
            'SettStatus': 'start',
            'Status': 'pending'
        }
    }
);

in your command, you're using $set as <options> in update command, not as part of <update>

http://docs.mongodb.org/manual/core/update/#crud-update-update

Upvotes: 61

Related Questions