Reputation: 28708
I am using node and mongo with the native client.
I would like to add pagination to my application.
To get pagination, I need my responses to always return count
alongside data
I would like to get something like:
{
count : 111,
data : [ { 'a' : 'only first item was requested' } ]
}
I can do this in mongo
> var guy = db.users.find({}).limit(1)
> guy.count()
11
> guy.toArray()
[
{
"_id" : ObjectId("5381a7c004fb02b10b557ee3"),
"email" : "[email protected]",
"fullName" : "guy mograbi",
"isAdmin" : true,
"password" : "fe20a1f102f49ce45d1170503b4761ef277bb6f",
"username" : "guy",
"validated" : true
}
]
but when I do the same with nodejs mongo client I get errors.
var cursor = collection.find().limit(1);
cursor.toArray( function(){ .. my callback .. });
cursor.count();
It seems that
count
is not defined on cursor toArray
on cursor, I cannot use the cursor againHow, using nodejs, can I accomplish the same thing I can with mongo directly?
Upvotes: 1
Views: 285
Reputation:
As others have said, if you want to have a total count of the items and then the data you will need to have two queries, there is no other way. Why are you concerned with creating two queries?
Upvotes: 1