0

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: example document

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?

1
  • there is no userId field in your screenshot.. Commented Jan 27, 2021 at 12:31

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.