简体   繁体   English

Mongoose 查询项目

[英]Mongoose querying for items

I Have a mongoose model for a coin that has我有一个硬币的猫鼬模型,它有

coinName: {
  type: String,
  required: true,
},
category: {
  type: String,
},

I want to get all the coinNames associated with a category我想获取与类别关联的所有 coinNames

Right now I have this code现在我有这个代码

const coins = await Coin.find().distinct('category',(error, categories) => {
 
})

which gives me all the categories of the coins.这给了我硬币的所有类别。
How can I get all the coins within a category?我怎样才能获得一个类别中的所有硬币?

Thank you谢谢

I don't fully understand what you want.我不完全明白你想要什么。 So if you want to group every coin by category, you could do it like this所以,如果你想按类别每硬币,你可以像下面这样

Or if you just want to find the coins that match one or more categories.或者,如果您只想找到与一个多个类别匹配的硬币。

try find expretion give filter尝试查找表达式给过滤器

const coins = await Coin.find({
  category: 'catagory name'
})

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

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