Reputation: 2246
I want to update records in mongoDB collection to a fixed limit. Is there a way to do it in PHP/mongoDB?
I know well about 'multi'
flag. when multi
is set true
it will update all matching records else update only one record.
But what I need is that I want to update records in a collection to a fixed limit like in SQL.
ex. UPDATE table SET x=10 wher x=1 LIMIT 10
I am wondering whether it is possible with mongoDB?
Hope I am clear with my question. Any help would be appreciated.
Upvotes: 1
Views: 386
Reputation: 43884
It is not currently possible to restrict an update query, only a find query, to MongoDB from your application.
The best way of doing this currently is to pull those ten (or whatever) out on client side, within your PHP application, and work on them there updating them:
$cursor = $db->col->find(array('x' => 1))->limit(10)
foreach($cursor as $row){
$row['x'] = 10;
$db->col->save($row); // Update with set can be used too
}
Providing it is a small number you can also do as @nw90 said, which is to grab the ObjectId
s out instead and then do that all in one query using an atomic $set
.
You will want to watch out for this: https://jira.mongodb.org/browse/SERVER-1599
Upvotes: 2
Reputation: 57
There is no such function as far as I know. You could do a workaround: Just fetch the _id's of the documents you want to update and update all documents with those _id's
Upvotes: 0