Sunday, 9 April 2017

mongodb find where field lower than another field plus parameter value

I have the following collection in mongodb, lets call it "products":

[
  {
    "name"           : "Product X",
    "warehouseStock" : 50,
    "reservedStock"  : 41
  },
  {
    "name"           : "Product Y",
    "warehouseStock" : 50,
    "reservedStock"  : 10
  }
]

I want to have a find() query that returns the documents in this collection where 'warehouseStock' is less than ('reservedStock' + threshold), where threshold is a parameter passed into the find() query.

I am trying to do the following find() query in node:

var threshold = 10;
mongo.getDb().collection('products').find({warehouseStock: {$lt:(threshold+'$reservedStock')}})

but this does not seem to work. I know I can do the following:

mongo.getDb().collection('products').find({warehouseStock: {$lt:threshold}})

but how can I query where

warehouseStock < (reservedStock + threshold)

So, if threshold was to be =10, then I would only get the first of the two items in the collection example above.

Thank you!



via Dreamlord

No comments:

Post a Comment