I need to fetch data from mongodb similer to the below sql query:
select * from ar_pages where (slug='ettarc' or id in ('1','2','3','4'))
Which uses one search condition and another with IN clause (fetch pages with specified slug and pages with list of ids)
I'm using Node Sails framework: I have tried below code: but it will not give the expected result
let listing = await Mymodel.find({
$or: [{
id: {
$in: ['5cxxxx90xxxx2e23xxxxxxxx', '5cxxxx18xxxx2e81xxxxxxxx']
}
}, {
user_id: 'xxxxxxxxx'
}]
});
The above code returns below error on execution:
MongoError: $or must be an array
Mongo DB Document sample:
{
"_id" : ObjectId("5d286f93c04a685616627095"),
"title" : "My First Page",
"description" : "My First Page Contents",
"user_id" : "5c80f410c1b9eb3d8fbf541c",
"status" : "active",
"createdAt" : 1562931091686.0,
"updatedAt" : 1563528420160.0,
"is_imported" : false,
"background_img" : "",
"background_color" : "",
"logo" : "",
"media_map" : null
}
Try this
db.ar_pages.find({
"$or": [{
"slug" : 'ettarc'
},{ "id": {"$in": ['1','2','3','4']}
}]
});
For Sails Framework use this:
db.ar_pages.find({
or: [
{
"slug" : 'ettarc'
}, {
"id": {
in: ['1','2','3','4']
}
}
]
});