How do I create a document field only if it doesn't already exist?

74 Views Asked by At

I would like to add a field to a document if it doesn't exist already but not update it if it does. Using mongodb directly I would use:

db.foo.update({'_created_by': {$exists : false}}, {$set: {'_created_by': 'x'}})

However, I am in Rust and my current code (using bson::document's insert) overwrites the field if it already exists:

doc.insert(format!("{}._created_by", key), bson!("x"));

How can I achieve the {$exists : false} part in Rust?

0

There are 0 best solutions below