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:

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

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -