I have a Query Collection,It's structure look like this
{
"_id": "5e71fa3ab004192b349e4a06",
"QUERIES": [
{
"_id": "5e71fa3ab004192b349e4a07",
"QUERY": "1",
"USER_ID": "5e6f1c5b8451307f782d0994",
"USER_NAME": "a1",
"createdAt": "2020-03-18T10:38:50.247Z"
},
{
"_id": "5e71fa46b004192b349e4a08",
"QUERY": "2",
"USER_ID": "5e6f1c5b8451307f782d0994",
"USER_NAME": "a2",
"createdAt": "2020-03-18T10:39:02.451Z"
}
],
"TICKET_ID": "5e70f4fa47df9479502f7937",
"USER_ID": "5e6f1c5b8451307f782d0994",
"RESOLVED_DATE": "2020-03-07T00:00:00.000Z"
}
I want delete first element from QUERIES array. Expected Output
{
"_id": "5e71fa3ab004192b349e4a06",
"QUERIES": [
{
"_id": "5e71fa46b004192b349e4a08",
"QUERY": "2",
"USER_ID": "5e6f1c5b8451307f782d0994",
"USER_NAME": "a2",
"createdAt": "2020-03-18T10:39:02.451Z"
}
],
"TICKET_ID": "5e70f4fa47df9479502f7937",
"USER_ID": "5e6f1c5b8451307f782d0994",
"RESOLVED_DATE": "2020-03-07T00:00:00.000Z"
}
I tried with the following update method but i got error while executing the update method on mongoDB Shell
db.getCollection("query_masters").update( {'QUERIES._id':ObjectId('5e726996b96d107eac5c88a4')},{$pull: { 'QUERIES._id': ObjectId('5e726996b96d107eac5c88a4') }}
)
Error Message
WriteResult({
"nMatched" : 0,
"nUpserted" : 0,
"nModified" : 0,
"writeError" : {
"code" : 28,
"errmsg" : "Cannot use the part (_id) of (QUERIES._id) to traverse the element"
})
-Thanks in advance
First I inserted data into my collection named Employee
and then I write query as:
db.Employee.update({'_id':("5e71fa3ab004192b349e4a06")},
{$pull:{"QUERIES": {USER_NAME:"a1"}}},false, true
);
Boolean values are upsert
and multi
you can check out in the documentation. And I just want to suggest you that use ObjectId
at your _id
above QUERIES
array
.