Using mongo's $inc type operator during an update/upsert ?

1,290 views
Skip to first unread message

Steven McDowall

unread,
Jul 9, 2014, 6:54:36 PM7/9/14
to loopb...@googlegroups.com
I have some "counting" columns that I would like to update in my mongoDB using the $inc type mongo thing .. but right now it's barfing on me using this code: (or variations like it) ... with an error code of :

updating tester data:  { id: 'VTESTERXA',

  testCellID: 'VTESTERXA',

  status: 'LOT ENDED',

  lastUpdatedTS: Wed Jul 09 2014 18:46:48 GMT-0400 (EDT),

  lot: 

   { failed: 0,

     passed: 3,

     total: 3,

     yield: 1,

     hbins: [ [Object], [Object] ],

     sbins: [ [Object], [Object] ] },

  '$inc': { batch: { failed: 0 } } }

got error:  { [MongoError: The dollar ($) prefixed field '$inc' in '$inc' is not valid for storage.]

  name: 'MongoError',

  lastErrorObject: 

   { connectionId: 161,

     err: 'The dollar ($) prefixed field \'$inc\' in \'$inc\' is not valid for storage.',

     code: 52,

     n: 0,

     ok: 1 },

  ok: 0,

  errmsg: 'The dollar ($) prefixed field \'$inc\' in \'$inc\' is not valid for storage.' }  and obj :  null




-=====


  // Map the attribute names into something the ones in the model ..
  var testCellData = {
    id : job.data.testCellID ,
    testCellID : job.data.testCellID ,
    status : "LOT ENDED" ,
    lastUpdatedTS : new Date(job.data.TIME_STAMP) ,
    lot : {
      failed: job.data.LOT_PARAMETERS.FAIL_COUNT,
      passed: job.data.LOT_PARAMETERS.PASS_COUNT,
      total: job.data.LOT_PARAMETERS.TOTAL_COUNT,
      yield: job.data.LOT_PARAMETERS.YIELD,
      hbins: job.data.LOT_PARAMETERS.HBINS,
      sbins: job.data.LOT_PARAMETERS.SBINS,
    },
    $inc : {
      batch : {
        failed : job.data.LOT_PARAMETERS.FAIL_COUNT,
      }
    }
  }

  if (! testCellData.lastUpdatedTS ) testCellData.lastUpdatedTS = Date.now() ;

  console.log("updating tester data: ", testCellData );
  TestCell.upsert( testCellData, function ( err, obj )  {
    if ( err ) {
      console.log("got error: " , err, " and obj : " , obj ) ;
    }
  } );


Any ideas? Is this supported? 

Raymond Feng

unread,
Jul 10, 2014, 12:04:48 PM7/10/14
to Steven McDowall, loopb...@googlegroups.com
LoopBack mongodb connector internally translate upsert as follows:


As a result, it won’t accept nested $inc.

I wonder if you should use dataSource.connector.collection(model).update(…) directly.

Thanks,

---
Raymond Feng
Co-Founder and Architect @ StrongLoop, Inc.

StrongLoop makes it easy to develop APIs in Node, plus get DevOps capabilities like monitoring, debugging and clustering.

--
You received this message because you are subscribed to the Google Groups "LoopbackJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to loopbackjs+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Steven McDowall

unread,
Jul 11, 2014, 11:06:46 AM7/11/14
to Raymond Feng, loopb...@googlegroups.com

I am wondering the same thing, and will probably do this for some of the more "complex" updates.

Thanks!

/steve

Pasindu De Silva

unread,
Mar 21, 2015, 2:57:41 PM3/21/15
to loopb...@googlegroups.com
probably allowExtendedOperators:true should work

Albert Frimpong

unread,
Mar 22, 2015, 6:02:50 AM3/22/15
to loopb...@googlegroups.com
where in the datasources.json file do you put that? Everywhere i've attempted failed. I logged the datasource in the console and it showed that in settings, allowExtendedOpeprators was true. I still get the MongoError: dollar $ prefixed

On Saturday, March 21, 2015 at 11:57:41 AM UTC-7, Pasindu De Silva wrote:
probably allowExtendedOperators:true should work

Pasindu De Silva

unread,
Mar 22, 2015, 7:32:22 AM3/22/15
to Albert Frimpong, loopb...@googlegroups.com

something like 

{
  "PowerDB": {
    "name": "PowerDB",
    "connector": "mongodb",
    "allowExtendedOperators":true
  }
}


On Sun, Mar 22, 2015 at 3:32 PM, Albert Frimpong <af.fr...@gmail.com> wrote:
where in the datasources.json file do you put that? Everywhere i've attempted failed.
On Saturday, March 21, 2015 at 11:57:41 AM UTC-7, Pasindu De Silva wrote:
probably allowExtendedOperators:true should work

--
You received this message because you are subscribed to a topic in the Google Groups "LoopbackJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/loopbackjs/zJtS8_4Etpw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to loopbackjs+...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Reply all
Reply to author
Forward
0 new messages