简体   繁体   English

猫鼬在特定条件下按索引在数组中查找值

[英]Mongoose find value in array by index with specific condition

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. 在这里,我找到第二个位置的数组字段“ dim”的值> 22&<30的文档。

I need to do the same thing with mongoose. 我需要对猫鼬做同样的事情。 but how?!? 但是如何?!?

You can use the same query with Mongoose. 您可以对Mongoose使用相同的查询。

Assuming that your Mongoose model is Inventory , this should work: 假设您的Mongoose模型是Inventory ,这应该可以工作:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM