user2101699
user2101699

Reputation: 257

MongoTemplate bulk operations ignores limit

I've got an employees collection that looks like:

{
_id
company_id
job_type
name
age
...
}

And a remove query that looks like:

val criteria = Criteria.where("company_id").isEqualTo(companyId).and("job_type").isEqualTo(jobType)
val query = Query(criteria).limit(3)

When I run it like:

mongoTemplate.remove(query, null, "collection_name")

It works great:)

Buy when I run it in Bulk Ops like:

val bulkOp = mongoTemplate.bulkOps(BulkOperations.BulkMode.UNORDERED, null, "collection_name")
bulkOp.remove(query)
...
bulkOp.execute()

It removes all of the documents for that company & job type while completely ignoring the limit=3

Is there a solution for the issue?

Upvotes: 2

Views: 824

Answers (1)

s7vr
s7vr

Reputation: 75934

Bulk writes with cursor options is not supported and is opposite for what bulk is designed for - Cursor is used for processing results in iterative fashion and make multiple trips to the server. On the other hand bulk updates are done in batch on server side.

The reason remove works when using mongo template is behind the scene it makes multiple queries one for fetching all the matches for query and picking up ids followed by another call to remove those ids.

You can also do the same by collecting ids before running remove query in mongodb for bulk.

Upvotes: 1

Related Questions