0

I call the Elements with find() method and after than i want to update all. For example:

db.collection.find().limit(10).update({$set: {'column' : 'value'}}); 

how can i fix this?

2 Answers 2

4

If you want to apply update to every document in collection, use {multi:true} option

db.collection.update({},{$set: {'column' : 'value'}},{multi:true}); 

For more detail, see collection.update

However, if you want to update selected number of documents, you'll be taking longer route.

db.collection.find().limit(10).forEach(function(o){
    o.column = some_value; // replace some_value with real one.
    db.collection.update({_id:o._id},o);
});
Sign up to request clarification or add additional context in comments.

5 Comments

thanks for reply but i got this error: db.collection.find(...).limit(...).update is not a function:
no, it doesn't work that way. I'm sorry I pasted your command as it is. try now.
@saleem, OP is asking for multi update with limit. The update you have there will update every document in the collection, and is not correct syntax either since the first argument should be query selector, not what to update
Well, to define limit there must be some condition. I don't think limit is applicable on update they way it's applicable on find.
However, only way to limit, is to call find with limit loop through cursor and update.
0

By default it updates only the first 1 document it found. You need to add multi = true as an option to update() to update all. Unfortunately, update() doesn't have limit option so you can limit it to 10.

You might have to do find() with limit first and then update each document separately like mentioned in this post:

How to limit number of updating documents in mongodb

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.