user156862
user156862

Reputation:

Mongodb findAndModify each document

I have a collection that contains a number of documents each document contains an array and I want to remove the lowest value from this Array for each document.

What would be the most efficient way to achieve this? Is findAndModify capable of performing this type of operation?

Do I need to use forEach?

Upvotes: 3

Views: 5797

Answers (2)

shelman
shelman

Reputation: 2699

JohnnyHK's comments are right. The only way to do this is to find() all the documents, update them individually, and resave them.

Upvotes: 4

Stephane Godbillon
Stephane Godbillon

Reputation: 1886

This is not the purpose of the FindAndModify command, like it is stated in the documentation:

The findAndModify command atomically modifies and returns a single document.

You should probably use the regular update method with the multi flag set to true. Check out the update documentation.

Upvotes: 3

Related Questions