Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove element from array in mongodb

Tags:

mongodb

I am new in mongodb and i want to remove the some element in array.

my document as below

{
  "_id" : ObjectId("4d525ab2924f0000000022ad"), 
  "name" : "hello", 
  "time" : [
      {
              "stamp" : "2010-07-01T12:01:03.75+02:00",
              "reason" : "new"
      },
      {
              "stamp" : "2010-07-02T16:03:48.187+03:00",
              "reason" : "update"
      },
      {
              "stamp" : "2010-07-02T16:03:48.187+04:00",
              "reason" : "update"
      },
      {
              "stamp" : "2010-07-02T16:03:48.187+05:00",
              "reason" : "update"
      },
      {
              "stamp" : "2010-07-02T16:03:48.187+06:00",
              "reason" : "update"
      }
  ]
}

in document, i want to remove first element(reason:new) and last element(06:00) .

and i want to do it using mongoquery, i am not using any java/php driver.

like image 751
javaamtho Avatar asked Feb 09 '11 13:02

javaamtho


People also ask

How do I remove an element from an array in MongoDB?

To remove an element from the array we use the operator $pull .

How do I edit an array in MongoDB?

Learn how to update array fields in documents in MongoDB collections. You can use the updateOne() or updateMany() methods to add, update, or remove array elements based on the specified criteria. It is recommended to use the updateMany() method to update multiple arrays in a collection.

What is addToSet in MongoDB?

The $addToSet operator adds a value to an array unless the value is already present, in which case $addToSet does nothing to that array. The $addToSet operator has the form: { $addToSet: { <field1>: <value1>, ... } } To specify a <field> in an embedded document or in an array, use dot notation.


2 Answers

If I'm understanding you correctly, you want to remove the first and last elements of the array if the size of the array is greater than 3. You can do this by using the findAndModify query. In mongo shell you would be using this command:

db.collection.findAndModify({
    query: { $where: "this.time.length > 3" },
    update: { $pop: {time: 1}, $pop: {time: -1} },
    new: true
});

This would find the document in your collection which matches the $where clause. The $where field allows you to specify any valid javascript method. Please note that it applies the update only to the first matched document.

You might want to look at the following docs also:

  1. http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-JavascriptExpressionsand%7B%7B%24where%7D%7D for more on the $where clause.
  2. http://www.mongodb.org/display/DOCS/Updating#Updating-%24pop for more on $pop.
  3. http://www.mongodb.org/display/DOCS/findAndModify+Command for more on findAndModify.
like image 195
asleepysamurai Avatar answered Sep 29 '22 00:09

asleepysamurai


You could update it with { $pop: { time: 1 } } to remove the last one, and { $pop: { time : -1 } } to remove the first one. There is probably a better way to handle it though.

like image 31
gnarf Avatar answered Sep 28 '22 22:09

gnarf