When building the query object for MongoDB, I encountered an issue with the output. Here is what I got:
output
{"$match":{"$and":[{id:1},{name:"Alfred"},{location:"moon"},{"values":{"$in":[{name:"u",age: 9}]} }]}}
Here is my code:
const queryFormat = (payload) => {
delete payload.userEventId
var query = {}
var res = []
for(key in payload) {
if(typeof(payload[key])) {
res.push({ [key]: payload[key] })
}
else {
res.push({"$in" :{ [key]: payload[key] }})
}
}
console.log(res[3])
query['$match'] = {"$and" :res}
console.log(query)
}
const payload = {
id :1,
name : 'Alfred',
location : 'moon',
values : [{name: 'u',age:9}]
}
queryFormat(payload)
expected output
{"$match":{"$and":[{id:1},{name:"Alfred"},{location:"moon"},{"values":{"$in":[{name:"u"},{age: 9}]} }]}}
Thank you!