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:
- http://www.mongodb.org/display/docs/advanced+queries#advancedqueries-javascriptexpressionsand%7b%7b%24where%7d%7d more on $where clause.
- http://www.mongodb.org/display/docs/updating#updating-%24pop more on $pop.
- http://www.mongodb.org/display/docs/findandmodify+command more on findandmodify.
Comments
Post a Comment