Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-1149

$inc operator not supported with $set operation in update

    XMLWordPrintableJSON

Details

    • Icon: Task Task
    • Resolution: Duplicate
    • Icon: Major - P3 Major - P3
    • None
    • 2.11.4
    • Write Operations

    Description

      I am trying to do an $inc operator from within the $set operation.
      When I try this using the shell, it works perfectly fine:

      db.S_M_users.update(

      { "sni" : 4 , "ki" : 0 , "ts" : 1184437800000, "snun" : "Amazon"}

      ,
      { $set:

      {"ki" : 0 , "sni" : 4 , "ts" : 1184437800000, "snu" : "UCWyLWwoPCLHHJp4NSXFV1Dw" , "snun" : "Amazon" , "ucm" : false , "uiu" : false}

      , $inc : { "oc" : 1}},true,false)

      But when I try to form the same query through java driver using update method it gets created like :
      query:

      { "sni" : 4 , "ki" : 0 , "ts" : 1184437800000 , "snun" : "Lorem ipsum dolor"}

      updateCriteria:
      { "$set" : { "ki" : 0 , "sni" : 4 , "ts" : 1184437800000 , "snu" : "UCWyLWwoPCLHHJp4NSXFV1Dw" , "snun" : "Lorem ipsum dolor" , "ucm" : false , "uiu" : false , "$inc" :

      { "oc" : 1}

      }}

      DB db = client.getDB("S_M_users");
      WriteResult wr = db.getCollection("S_M_users").update(query, updateCriteria, true, false, 1);

      db.S_M_users.update(

      { "sni" : 4 , "ki" : 0 , "ts" : 1184437800000 , "snun" : "Lorem ipsum dolor"}

      ,
      { "$set" : { "ki" : 0 , "sni" : 4 , "ts" : 1184437800000 , "snu" : "UCWyLWwoPCLHHJp4NSXFV1Dw" , "snun" : "Lorem ipsum dolor" , "ucm" : false , "uiu" : false , "$inc" :

      { "oc" : 1}

      }},true,false)

      And in java driver I get following exception:
      Error while updating record: { "serverUsed" : "CTPLSRV028:27017" , "err" : "Modified field name may not start with $" , "code" : 15896 , "n" : 0 , "lastOp" :

      { "$ts" : 1395147947 , "$inc" : 1}

      , "connectionId" : 19680328 , "ok" : 1.0}

      There should be a way to use $inc from within a set operation using Java Driver

      Attachments

        Activity

          People

            Unassigned Unassigned
            dj1987 Divya Jain
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: