I want to write a mongodb query that allows me to fetch array objects fields based on the IN/OR condition like in relational databases. If I have the following document in my collection, I want to read all "events.event" in ('user','bot')
{_id: 1,
sender_id:100,
"events" : [
{
"event" : "action",
"timestamp" : 1619463803.7244627,
"name" : "abc1"
},
{
"event" : "user",
"timestamp" : 1619463803.7244627,
"name" : "abc2"
},
{
"event" : "bot",
"timestamp" : 1619463803.7244627,
"name" : "abc3"
}
}
I used the following query but it only works for ONE event at a time. Can this be modified to
consider event = 'user' or event = 'bot' ? And can we also project events.event and events.timestamp
along with this elemMatch; all in one go?
db.conversations.find(
{"events.event": "bot"},
{_id: 0, sender_id:1, events: {$elemMatch: {event: "bot"}}});
$filteraggregation expression operator in your projection.