Reputation: 4666
I am trying to update documents of collection sale_order if sale_order['orderId']
exists in temp_sale_order
. If it does not exists in temp_sale_order
then a new document will be created in sale_order
. (upsert).
But it only creates only one document in it.
If i use find
instead of findOne
. it throws an error:
Error:
uncaught exception: can't save a DBQuery object
My query:
db.getCollection('sale_order').update(
db.getCollection('sale_order').find({},{orderId:1}),
db.getCollection('temp_sale_order').findOne({},{orderId:1}),{upsert:true});
what is the correct way? Also how to write and use a function in mongodb with above query?
Upvotes: 1
Views: 2073
Reputation: 887
By default, update
method updates only one document.
You have to set the option "multi" : true
to update multiple documents at once.
http://docs.mongodb.org/manual/reference/method/db.collection.update/
db.getCollection('sale_order').update(
db.getCollection('sale_order').find({},{orderId:1}),
db.getCollection('temp_sale_order').findOne({},{orderId:1}),{upsert:true, multi:true});
Upvotes: 1