Caio César Caio César - 5 months ago 30
Java Question

Remove multiple array Entries on Mongo using MongoOperations

I already can execute the desired query on mongoshell, but i need to make the same query using Java and MongoOperations.

I have checked this question, which is very similar, but it only has one condition, as mine has two and uses the

operators. Here's the working mongo Query:

db.getCollection('example').update({"idVar": "desiredValued"}, { $pull: { "listaHoras": { $gte: ISODate("2016-11-06T05:50:00.000Z"), $lt: ISODate("2016-11-06T06:30:00.000Z")}}})

Sample doc:

"_id" : ObjectId("58221b4610a3c71f1894ce75"),
"idVar" : "56b11259272f5515b05d70bc",
"date" : ISODate("2016-11-06T03:00:00.000Z"),
"listaHoras" : [

Where i'll have the
as a
variable in Java, and the
as a String variable.

So far, i have i did the following, using the previously mentioned question as example:

BasicDBObject match = new BasicDBObject("idVar", desiredValue); // to match your document
BasicDBObject update = new BasicDBObject("listaHoras", new BasicDBObject("itemID", "1"));
coll.update(match, new BasicDBObject("$pull", update));

But, as you can see, this is NOT equivalent to the desired query. Since the match for the
is matching
with "1". I do not know, nor was i able to find how to properly use the
on the same query. Neither on how to use just one or both of them. I know it CAN be done as seen on the MongoOperatioons API which says:

"update - the update document that contains the updated object or $ operators to manipulate the existing object."

Anyone knows how it can be done? And if the
type in Java matches the
on the Mongo?


I managed to find a solution. It is similar to what Veeram posted as a answer but it's slightly different. I simply removed his updateCriteria and used a BasicDBObject in it's place.

Here's how the full code looks:

Query findQuery = new Query();
Criteria findCriteria = Criteria.where("idVar").is(idVar);
Update update = new Update().pull("listaHoras", new BasicDBObject( "$gte", start).append("$lte", end));
mongoOps.updateMulti(findQuery, update, "CollectionName");

Where start and end are Date variables recieved by the method. Also important to note that Mongo uses the UTC as default timezone, so we must properly format the time in order for it to remove the desired values.