Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Filter mongoose array with an array?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

...................
...................
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?

>Solution :

you are going good, this is the correct way

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading