1

I am trying to return documents where at least one element from the selected field array matches the query array.

I have tried the following queries:

const match_equipment = await studios.find({equipment: {$eleMatch:{$in: ["mic","amp"]}}});
const match_equipment = await studios.find({equipment: {$all: ["mic","amp"]}});

The 1st query gives me "db query failed error" 2nd one gives me "no result found".

Sample document:

{
    "_id": {"$oid": "5ec5d38be97c8869bee29e78"},
    "equipment": ["guitar", " mic", " amp"],
    "studio_name": "Best recordings",
    "email": "changed@gmail.com",
    "address": "34 harming st. Brunswick",
    "postcode": "3056",
    "price": "50",
    "__v": {"$numberInt": "0"},
    "unavalibility": [
        {
            "times": [{"$numberInt": "13"}],
            "_id": {"$oid": "5ec610e47cf2fb4e84ba0c54"}, "date": {"$date": {"$numberLong": "1591920000000"}}
        }
    ]
}
Tom Slabbaert
  • 21,288
  • 10
  • 30
  • 43
Iris Lin
  • 11
  • 2
  • The second one should work, you can see a working example here - https://mongoplayground.net/p/l8tqUZY5xAU – Puneet Singh May 22 '20 at 09:24
  • Please do not use images for code or text but add relevant code fragments or listings to you question. – rveerd May 22 '20 at 09:59
  • sorry first time code; didn't know how it should work. i will copy paste my code – Iris Lin May 22 '20 at 11:59
  • 1
    @PuneetSingh thank you! i found out the problem, it's my document actually has a space before "mic" as " mic". i will try add regexp ! – Iris Lin May 22 '20 at 12:27
  • Sorry follow up question, how should i add regular expression for fuzzy search? i am not very familiar with it but tried this did't work. https://stackoverflow.com/questions/38421664/fuzzy-searching-with-mongodb eg. if i want document with "DRUM" or " drum" can all be found when query has "drums" "drumset" ? – Iris Lin May 22 '20 at 12:40

1 Answers1

0

You are very close, you just have a syntax error.

It's $elemMatch and not eleMatch

So changing it will work:

const match_equipment = await studios.find({equipment: {$elemMatch:{$in: ["mic","amp"]}}});

But the essence of $elemMatch is more specialised queries. Which in your case are not required. So you can just use the following version utilizing $in only:

const match_equipment = await studios.find({equipment: {$in: ["mic","amp"]}});
Tom Slabbaert
  • 21,288
  • 10
  • 30
  • 43