Converting a MongooseCollection into a normal array

11,468 views
Skip to first unread message

WilsonPage

unread,
May 4, 2012, 4:50:40 AM5/4/12
to mongoo...@googlegroups.com
I want to be able to turn a Mongoose Collection into a normal array. I have heard there is a method .toArray(). I have tried calling this on the Mongoose Collection directly and on Model.find({}).toArray(function(err, docs) {}); Both seem to throw error 'has no method toArray()'. 

What am I doing wrong?

Aaron Heckmann

unread,
May 4, 2012, 9:49:10 AM5/4/12
to mongoo...@googlegroups.com
toArray() is a method of the drivers Cursor class. If I understand the question you can retrieve all docs in a collection by

YourModel.find({}, function (err, docs) {
  // docs is an array of all docs in collection
});

On May 4, 2012, at 1:50 AM, WilsonPage <wilsons...@gmail.com> wrote:

I want to be able to turn a Mongoose Collection into a normal array. I have heard there is a method .toArray(). I have tried calling this on the Mongoose Collection directly and on Model.find({}).toArray(function(err, docs) {}); Both seem to throw error 'has no method toArray()'. 

What am I doing wrong?

--
http://mongoosejs.com
http://github.com/learnboost/mongoose
You received this message because you are subscribed to the Google
Groups "Mongoose Node.JS ORM" group.
To post to this group, send email to mongoo...@googlegroups.com
To unsubscribe from this group, send email to
mongoose-orm...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/mongoose-orm?hl=en

WilsonPage

unread,
May 4, 2012, 1:02:21 PM5/4/12
to mongoo...@googlegroups.com
But the array returned still has all the hidden Mongoose methods and properties. I want to send this collection to the client and am currently having to do JSON.stringify(JSON.parse(docs)) to strip the array of all the Mongoose magic. Just wondering if there was a cleaner was of doing this?


On Friday, 4 May 2012 14:49:10 UTC+1, Aaron Heckmann wrote:
toArray() is a method of the drivers Cursor class. If I understand the question you can retrieve all docs in a collection by

YourModel.find({}, function (err, docs) {
  // docs is an array of all docs in collection
});

On May 4, 2012, at 1:50 AM, WilsonPage <wilsons...@gmail.com> wrote:

I want to be able to turn a Mongoose Collection into a normal array. I have heard there is a method .toArray(). I have tried calling this on the Mongoose Collection directly and on Model.find({}).toArray(function(err, docs) {}); Both seem to throw error 'has no method toArray()'. 

What am I doing wrong?

--
http://mongoosejs.com
http://github.com/learnboost/mongoose
You received this message because you are subscribed to the Google
Groups "Mongoose Node.JS ORM" group.
To post to this group, send email to mongoo...@googlegroups.com
To unsubscribe from this group, send email to

Aaron Heckmann

unread,
May 4, 2012, 2:11:37 PM5/4/12
to mongoo...@googlegroups.com
You'll need to stringify() the data before sending. 

res.send(JSON.stringify(docs))

If just want to covert to vanilla js then call docs.toObject() directly.

Anthony Bullard

unread,
Jun 2, 2012, 1:32:07 AM6/2/12
to mongoo...@googlegroups.com
Aaron, just so you know I found out today actually that an array of documents returned from a Model.find() actually has no toObject() method.  Only individual documents do.  I'm write now creating a workaround for this.  I'd like to submit this as a pull request, but I don't even know where to begin as far as putting it in the source.

Aaron Heckmann

unread,
Jun 2, 2012, 2:50:15 PM6/2/12
to mongoo...@googlegroups.com
oh right yeah thanks. there is no toObject method of the array returned from Model.find b/c its already a vanilla js array.

no need for a pull request. i don't want to subclass the array just to add toObject on it which would merely be

   return docs.map(function (d) { return d.toObject() })


Anthony Bullard

unread,
Jun 2, 2012, 4:06:56 PM6/2/12
to mongoo...@googlegroups.com
True.  This is basically exactly what I did.
Reply all
Reply to author
Forward
0 new messages