crcn / sift.js

Use Mongodb queries in JavaScript
MIT License
1.65k stars 101 forks source link

Implement $expr operator #237

Open omanizer opened 3 years ago

omanizer commented 3 years ago

This may be a can of worms, but it would be a very nice addition to this library. It would be nice to do a direct comparison on a record's fields with each other rather than only comparing a single column with a static value.

In SQL this functionality is trivial:

SELECT * from my_table WHERE points_total > points_used

Mongo provides the $expr operator for this purpose, which is a newer feature:

db.my_collection.find({
  $expr: {
    $gt: ['$points_total', '$points_used']
  }
})

This allows finding all records that have points remaining without having to manually implement a points_remaining column. It's technically an "aggregation" functionality but since it's been made available in the find operation it could make sense to include here as well.

Thoughts?

crcn commented 2 years ago

Yeah I think this would be great! Though, I don't have much time to work on this right now. Happy merge a PR into Sift though.

kethan commented 3 months ago

https://github.com/kethan/uqry

You can try this under 1kb with aggregation. It has $expr too.