set a value only if not already set

XMLWordPrintableJSON

    • Type: New Feature
    • Resolution: Won't Do
    • Priority: Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Write Ops
    • None
    • Query
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      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(

      {uid: 15}

      , { $init :

      { status : "offline" }

      }
      // the status field wasn't set so it is initialized

      {uid: 15, name: "myname", status: "offline"}

      db.users.update(

      {uid: 15}

      , { $set :

      { status : "online" }

      }
      db.users.update(

      {uid: 15}

      , { $init :

      { status : "offline" }

      }
      // no effect because the status field was already set.

      {uid: 15, name: "myname", status: "online"}

      Note that $not and $exists won't help in case you want to set some fields and init others:
      db.users.update(

      {uid: 15}

      , { $init :

      {status : "offline" }

      , $set:

      {age: 23}

      }

            Assignee:
            Backlog - Query Team (Inactive)
            Reporter:
            pablo platt
            Votes:
            1 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: