Remove item from array in mongodb

I am new to mongodb and I want to delete some element in an array.

my document is 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 the document, I want to delete the first element (reason: new) and the last element (06:00).

and I want to do this with mongoquery, I do not use the java / php driver.

+4
source share
5 answers

If I understand 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 using the findAndModify query. In the mongo shell, you will use this command:

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

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

You can also see the following documents:

+6
source

You can update it with { $pop: { time: 1 } } to delete the last, and { $pop: { time : -1 } } to delete the first. There is probably a better way to handle this.

+4
source

@javaamtho you cannot check a size greater than 3, but only if it is exactly 3, for a size greater than x, you should use the $inc operator and have a field in which you are either 1 or -1 to keep track of when you delete or add elements (use a separate field outside the array, as shown below, time_count)

 { "_id" : ObjectId("4d525ab2924f0000000022ad"), "name" : "hello", "time_count" : 5, "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" } ] } 
0
source

If you want to leave these time elements, you can use the aggregate command from mongo 2.2+ to extract the minimum and maximum time elements, undo all the time elements and press the minutes and maximum versions (with some changes, it could do your job):

 smax=db.collection.aggregate([{$unwind: "$time"}, {$project: {tstamp:"$time.stamp",treason:"$time.reason"}}, {$group: {_id:"$_id",max:{$max: "$tstamp"}}}, {$sort: {max:1}}]) smin=db.collection.aggregate([{$unwind: "$time"}, {$project: {tstamp:"$time.stamp",treason:"$time.reason"}}, {$group: {_id:"$_id",min:{$min: "$tstamp"}}}, {$sort: {min:1}}]) db.students.update({},{$unset: {"scores": 1}},false,true) smax.result.forEach(function(o) {db.collection.update({_id:o._id},{$push: {"time": {stamp: o.max ,reason: "new"}}},false,true)}) smin.result.forEach(function(o) {db.collection.update({_id:o._id},{$push: {"time": {stamp: o.min ,reason: "update"}}},false,true)}) 
0
source

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

convert to php

0
source

Source: https://habr.com/ru/post/1339033/


All Articles