Home > Enterprise >  Filter mongoose array with an array?
Filter mongoose array with an array?

Time:12-06

I am facing difficulties when I am trying to apply mongoose filter to an array element. I found one documentation about $elemMatch, but I am huge confused that it is the best approach with my condition. That's why I asking a questions.

Thank you very much--

Here I have mongoDB document with tags. Actually it is an array.

...................
...................
tag: [
  {name: "fashion"},
  {name: "mobile"}
]
...................
...................

Images is here- Click to see images.

Again I am getting an array from forntend app like-

{
  tags: [
    {name: "fashion"},
    {name: "Computer}
 ]
}

Now I have to filter if match just one or more tag. How can I apply it.

I am trying it with elemMatch like this

await Product.find(tags: {$elemMatch: {name: {$in: ["Fashion", "Computer"]}}}

Is it right or I have to change my code?

CodePudding user response:

you are going good, this is the correct way

  • Related