4

What is the big-o complexity of MongoDB lookup operation?
Say I have n records in my MongoDB collection 'A',
And n records in collection 'B', each B's document have foreign _id of A, and one specific tag eg "preOrder","directOrder", "pendingOrder" ...

case 1:
if I add match query on the collection 'A' with some filters and add lookup with B collection.
and I got resulted in an array of objects of B's data and I loop through each array of each object to performs some operation and count each tag's count

case 2:
Add match query on the collection 'A' with filter
loop through each result and query in the loop for match B's record

What will be the difference in time and load on the mongo server?

Fazal
  • 300
  • 2
  • 14

1 Answers1

1

Your question depends on whether an index can be used for the query criteria of your find or not. If an index can be used, it also depends on the type of index:

If no index can be used, you can bet on O(n).

In most cases, indexes are b-trees, in which case you can expect O(log n).

In the special case that a hash index can be used, and provided your query looks for the exact value, it could be O(1).

You can use abc.explain() ton analyse the query execution plan (COLLSCAN O(1) vs IXSCAN index type specific big-O).