Remove element from array in mongodb -


i new in mongodb , want remove element in array.

my document 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, want remove first element(reason:new) , last element(06:00) .

and want using mongoquery, not using java/php driver.

if i'm understanding correctly, want remove first , last elements of array if size of array greater 3. can using findandmodify query. in mongo shell using command:

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

this find document in collection matches $where clause. $where field allows specify valid javascript method. please note applies update first matched document.

you might want @ following docs also:

  1. http://www.mongodb.org/display/docs/advanced+queries#advancedqueries-javascriptexpressionsand%7b%7b%24where%7d%7d more on $where clause.
  2. http://www.mongodb.org/display/docs/updating#updating-%24pop more on $pop.
  3. http://www.mongodb.org/display/docs/findandmodify+command more on findandmodify.

Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -