简体   繁体   中英

Matching nested array of nested subdocuments in MongoDB

I am trying to create a query, that matches two criteria. Match a specific userId and match a range of Id's.

An example document could look like this: 示例文档

I have tried with using find by adding the userId and then using the MongoDB $in operator to match a list of feature (as an id)

  const featureIds = features.map(feature => feature._id)
    console.log(featureIds)
    const isFeatureExists = await this.userShopModel.find({
      userId,
      'items.feature': { $in: featureIds },
    })
    console.log(isFeatureExists)

I have also tried with different versions of aggregate but without any luck.

Any mongo experts, who can help me?

use _id for filter

const featureIds = features.map(feature => feature._id)
    console.log(featureIds)
    const isFeatureExists = await this.userShopModel.find({
      _id : userId,
      'items.feature': { $in: featureIds },
    })
    console.log(isFeatureExists)

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