Does anyone who knows how to filter data and only keep those history_doc.data.objectId equals _id?
I have tried so many methods but none of them works
{'history_doc.data.objectId': {$eq: '$_id'}}
{'history_doc.data.objectId': {$eq: {$toString: '$_id'}}}
2
You can use $expr and $eq with $toObjectId into aggregation query like this:
$expr
$eq
$toObjectId
.aggregate({ $match: { $expr: { $eq: [ "$_id", { "$toObjectId": "$history_doc.data.objectId" } ] } } })
Example here.
You can do it with Aggregation Framework:
$match
$toString
_id
history_doc.data.objectId
db.collection.aggregate([ { "$match": { "$expr": { "$eq": [ { "$toString": "$_id" }, "$history_doc.data.objectId" ] } } } ])
Working example
Click here to cancel reply.
2
Answers
You can use
$expr
and$eq
with$toObjectId
into aggregation query like this:Example here.
You can do it with Aggregation Framework:
$match
– to filter documents based on some custom criteria$eq
and$toString
– To check it_id
andhistory_doc.data.objectId
are the same.Working example