node.js - MongoDB : querying documents with two equal fields, $match and $eq -
what best way return documents in collection if want document.a == document.b?
i've tried
db.collection.aggregate([ { $match: { $eq: [ '$a', '$b' ] } }])
but returns no errors or results, because assume literally matching strings "$a" , "$b". there different way specify these fields?
db.collection.aggregate([ { $project: { eq: { $cond: [ { $eq: [ '$a', '$b' ] }, 1, 0 ] } } }, { $match: { eq: 1 } }])
the above works, requires additional step of querying again whatever documents found or projecting possible fields.
is there better way achieving query?
basically, trying perform self join. operation not supported mongodb.
concerning $eq
operator, guessed:
- by design,
$eq
comparison query operator match field against value. - but
$eq
comparison aggregation operator compare value of 2 expressions.
i don't know other way perform need using $project
step suggested.
please note not significantly more expensive as, anyway, query cannot use index , mongodb full scan.
Comments
Post a Comment