Learn, Share, Build

125
October 09, 2017, at 06:32 AM

I need to create a query that find a document in which each of the values of an array field must meet a different condition. Those condition differ by the index of this array.

i read that it can be done, for example, in this way:

var cursor = db.collection('inventory').find({ 
  "dim.1": { $gt: 22, $lt: 30 }
});

here i find document in which the array field "dim" has a value >22 & <30 in the second position.

I need to do the same thing with mongoose. but how?!?

Answer 1

You can use the same query with Mongoose.

Assuming that your Mongoose model is Inventory, this should work:

Inventory.find({ 
  "dim.1": { $gt: 22, $lt: 30 }
}, function(err, docs) {
  // Callback code
})

http://mongoosejs.com/docs/models.html

READ ALSO
Learn, Share, Build

Learn, Share, Build

Take a look at this JSFiddle example in Chrome

100
Learn, Share, Build

Learn, Share, Build

This question already has an answer here:

87
Learn, Share, Build

Learn, Share, Build

I'm currently developing a web ui

105