Is there a way to find the length of an array in MongoDB without returning the entire array?

Tags: arrays mongodb
Question!

I'm paginating a list of embedded documents, and I'm wondering if there's a cheap way to find out the length of that list, analogous to count() for documents . I know that

array.length

works, but I'm guessing that's it's slow and inefficient.

Any advice would be much appreciated.

By : Alistair


Answers

Maintain a dedicated counter of the array as part of the document. There is no other efficient way to determine the length of an array.



This video can help you solving your question :)
By: admin