Details
-
New Feature
-
Resolution: Won't Do
-
Major - P3
-
None
-
None
-
None
-
Query
Description
I suggest a feature that allows to set a value to a field only if it is not already defined.
This could be similar to $addToSet and might be called $init:
db.users.insert(
{uid: 15, name: "myname"})
db.users.update(
, { $init :
{ status : "offline" } }
// the status field wasn't set so it is initialized
db.users.update(
{uid: 15}, { $set :
{ status : "online" } }
db.users.update(
, { $init :
{ status : "offline" } }
// no effect because the status field was already set.
Note that $not and $exists won't help in case you want to set some fields and init others:
db.users.update(
, { $init :
{status : "offline" }, $set:
{age: 23}}