简体   繁体   中英

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

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'
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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