2

I have a few documents in the collection orders where is_show is already set with value true/false for the new documents .

Now what I want is , for the past documents , it should insert the key
is_show with value false for particular order type . For that I have written the query as shown below :

db.getCollection("orders").update({"order_type":1}, {$set: {"is_show": false}}, false, true) 

But some how it is adding is_show false for the new as well as old documents .

so how can I modify the query to achieve that functionality?

1 Answer 1

4

You can use $exists query operator to check whether the document contains that field or not

db.getCollection("orders").update( { "order_type": 1, "is_show": { "$exists": false }}, { "$set": { "is_show": false }}, { "multi": true } ) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.