I have three documents that look like this
{
"name": "X1",
"location": {
"country": "Ireland",
"state": "Dublin",
},
},
{
"name": "X2",
"location": {
"country": "Ireland",
"state": "Dublin",
"address": ""
},
},
{
"name": "C3",
"location": {
"country": "United States of America",
"state": "California",
"address": "San Jose"
},
I am trying to implement a filtering functionality to show documents based on multiple locations.
so, the endpoint URL looks like this for example http://localhost:3000/api/companies/stacks?state[]=Dublin&country[]=United%20States%20of%20America
So, it should return the three documents that are shown above, yet it only returns this document
{
"name": "C3",
"location": {
"country": "United States of America",
"state": "California",
"address": "San Jose"
},
Here is the source code, I am using Mongoose mainly.
//Filtering by location
if ('country' in req.query) {
const countries = [...country]
conditions['location.country']={$in:countries}
}
if ('state' in req.query) {
const states = [...state]
conditions['location.states'] = { $in: states }
}
const doc = await model
.find(conditions)
.sort({ p: -1, _id: 1 })
.lean()
.exec()
I understood that it returns the document that matches the country parameter, but I don't understand why it doesn't also fetch the other documents that has Dublin in states?
Edit: I also tried to use $or operator like that
find({
$or:[
{'location.country':'United States of America'},
{'location.states':'Dublin'}
]
})
Yet it also, returned the document that matches the country only. I thought about using aggregates but I am not sure if it is going to help me in my case?
Thanks in advance.