Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-1088

set a value only if not already set

    XMLWordPrintableJSON

Details

    • Icon: New Feature New Feature
    • Resolution: Won't Do
    • Icon: Major - P3 Major - P3
    • None
    • None
    • Write Ops
    • 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(

      {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}

      }

      Attachments

        Activity

          People

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

            Dates

              Created:
              Updated:
              Resolved: