Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a field from all elements in array in mongodb [duplicate]

Tags:

arrays

mongodb

I have below document in MongoDB(2.4.5)

{     "_id" : 235399,     "casts" : {         "crew" : [              {                 "_id" : 1186343,                 "withBase" : true,                 "department" : "Directing",                 "job" : "Director",                 "name" : "Connie Rasinski"             },             {                 "_id" : 86342,                 "withBase" : true             }         ]     },     "likes" : 0,     "rating" : 0,     "rating_count" : 0,     "release_date" : "1955-11-11" } 

I want to remove withBase filed from array elements inside casts.crew ..

I tried this

db.coll.update({_id:235399},{$unset: {  "casts.crew.withBase" : 1 } },false,true) 

nothing changed.

And tried this..

db.coll.update({_id:235399},{$unset: {  "casts.crew" : { $elemMatch: { "withBase": 1 } } } },false,true) 

it removed entire crew array from the document.

Can someone please provide me the right query?

like image 489
Nancy Avatar asked Nov 13 '13 05:11

Nancy


People also ask

How do I remove a field from an array in MongoDB?

To remove an element, update, and use $pull in MongoDB. The $pull operator removes from an existing array all instances of a value or values that match a specified condition.

How do you remove duplicate values from an array?

We can remove duplicate element in an array by 2 ways: using temporary array or using separate index. To remove the duplicate element from array, the array must be in sorted order. If array is not sorted, you can sort it by calling Arrays. sort(arr) method.

How do I remove duplicates in MongoDB?

General idea is to use findOne https://docs.mongodb.com/manual/reference/method/db.collection.findOne/ to retrieve one random id from the duplicate records in the collection. Delete all the records in the collection other than the random-id that we retrieved from findOne option.

How do you remove all duplicates from an array of objects?

To remove the duplicates from an array of objects:Use the Array. filter() method to filter the array of objects. Only include objects with unique IDs in the new array.


2 Answers

You can use the new positional identifier to update multiple elements in array in 3.6.

Something like

 db.coll.update( {_id:235399}, {$unset: {"casts.crew.$[].withBase":""}} ) 

$[] removes all the withBase property from the crews array. It acts as a placeholder for updating all elements in array.

Use multi true to affect multiple documents.

like image 187
s7vr Avatar answered Sep 16 '22 14:09

s7vr


Sorry to disappoint you, but your answer

db.coll.update({    _id:235399,    "casts.crew.withBase": {$exists: true} },{    $unset: {       "casts.crew.$.withBase" : true    } },false,true) 

is not correct. Actually it will remove the value, BUT only from the first occurrence of the subdocument, because of the way positional operator works:

the positional $ operator acts as a placeholder for the first element that matches the query document

You also can not use $unset (as you tried before) because it can not work on arrays (and are you basically trying to remove a key from a document from the array). You also can not remove it with $pull, because pull removes all the array, not just a field of it.

Therefore as far as I know you can not do this with a simple operator. So the last resort is doing $find and then forEach with save. You can see how to do this in my answer here. In your case you need to have another loop in forEach function to iterate through array and to delete a key. I hope that you will be able to modify it. If no, I will try to help you.

P.S. If someone looks a way to do this - here is Sandra's function

db.coll.find({_id:235399}).forEach( function(doc) {   var arr = doc.casts.crew;   var length = arr.length;   for (var i = 0; i < length; i++) {     delete arr[i]["withBase"];   }   db.coll.save(doc); }); 
like image 37
Salvador Dali Avatar answered Sep 18 '22 14:09

Salvador Dali