Home > Net >  How to filter based on a property of an inner object in mongoose?
How to filter based on a property of an inner object in mongoose?

Time:10-04

    const collection = [
      {
        inner_obj: {
          prop: "A"
        }
      }
    ]

    collection.find(
      //code to find outer records based on the `prop` property of column `inner_obj`
    )

how to find outer records based on the prop property of column inner_obj.

CodePudding user response:

You can use Array.filter to filter out the items in the array which don't match the condition:

const desiredProp = "A"

const collection = [{
  inner_obj: {
    prop: "A"
  }
},
{
  inner_obj: {
    prop: "B"
  }
}]

const filtered = collection.filter(e => e.inner_obj.prop == desiredProp)

console.log(filtered)

CodePudding user response:

You can try this

collection.find({"inner_obj.prop": "A"})
  • Related