1

Simple find or where queries can also be written as aggregate queries using $match. Which of these is a better and faster approach to get the result from the MongoDB? E.g.

  1. Boy.where(name: 'Ankit')
  2. Boy.collection.aggregate([{'$match': {name: 'Ankit'}}])

Can someone please point out the difference in both the queries performance wise?

Ankit Agrawal
  • 99
  • 1
  • 5
  • i think this link will help you [different b/w find and wher](https://stackoverflow.com/questions/15185919/whats-the-difference-between-find-where-and-find-by-id) – adarsh Nov 29 '18 at 12:03
  • Thanks for responding. But, I do understand the mentioned link. What I don't understand is should I be using `aggregate` to get results which can be achieved by using `where` or `find`? – Ankit Agrawal Nov 30 '18 at 09:12
  • the better way is to follow the Active Record patterns which will help any Rails developer to be productive in your code quickly. if your concern is on performance then do some benchmarks, but I hope you know what premature optimization is. – Oshan Wisumperuma Jan 24 '19 at 17:19
  • Possible duplicate of [difference between aggregate ($match) and find, in MongoDB?](https://stackoverflow.com/questions/16552433/difference-between-aggregate-match-and-find-in-mongodb) – ashusvirus Mar 28 '19 at 09:27
  • Does this answer your question? [what is difference between db.collection.find() and db.collection.aggregation()](https://stackoverflow.com/questions/35927371/what-is-difference-between-db-collection-find-and-db-collection-aggregation) – Tom Slabbaert Oct 25 '21 at 14:19

0 Answers0