2

I want to send my MongoDB collection results to a service -( Algolia to be precise) as an array.

The results MongoDB return are not array in format. So am thinking if there is a way to convert the collection result into an array.

This is what it returns:

{ _id: 5b30c318ca1ea60cb8a55d2f,
  memeid: 'Z3Q7NC',
  url: 'http://res.cloudinary.com/memeafrica/image/upload/v1529922328/test/adam-levine_nxqpnt.gif',
  tags: 'laugh,laughing,laughing uncontrollaby ',
  caption: '',
  imgs: 'adam-levine.gif',
  __v: 0,
  uploadDate: 2018-06-25T10:25:28.940Z,
  tagarray: [ 'laugh', 'laughing', 'laughinguncontrollaby' ],
  views: 500 }
{ _id: 5b30c759ca1ea60cb8a55d30,
  memeid: '2oIjDP',
  url: 'http://res.cloudinary.com/memeafrica/image/upload/v1529923417/test/Debffc4XkAAuvLj_mvzvpz.jpg'
  tags: 'zuma',
  caption: '',
  imgs: 'Debffc4XkAAuvLj.jpg',
  __v: 0,
  uploadDate: 2018-06-25T10:43:37.859Z,
  tagarray: [ 'zuma' ],
  views: 2000 }
{ _id: 5b30d4b22904771be030db62,
  memeid: 'eLT1F',
  url: 'http://res.cloudinary.com/memeafrica/image/upload/v1529926834/test/crying_b6fjaf.gif',
  tags: 'laugh',
  caption: '',
  imgs: 'crying.gif',
  __v: 0,
  uploadDate: 2018-06-25T11:40:34.649Z,
  tagarray: [ 'laugh' ],
  views: 0 }

What am expecting:

[
{ _id: 5b30c318ca1ea60cb8a55d2f,
  memeid: 'Z3Q7NC',
  url: 'http://res.cloudinary.com/memeafrica/image/upload/v1529922328/test/adam-levine_nxqpnt.gif',
  tags: 'laugh,laughing,laughing uncontrollaby ',
  caption: '',
  imgs: 'adam-levine.gif',
  __v: 0,
  uploadDate: 2018-06-25T10:25:28.940Z,
  tagarray: [ 'laugh', 'laughing', 'laughinguncontrollaby' ],
  views: 500 }
{ _id: 5b30c759ca1ea60cb8a55d30,
  memeid: '2oIjDP',
  url: 'http://res.cloudinary.com/memeafrica/image/upload/v1529923417/test/Debffc4XkAAuvLj_mvzvpz.jpg'
  tags: 'zuma',
  caption: '',
  imgs: 'Debffc4XkAAuvLj.jpg',
  __v: 0,
  uploadDate: 2018-06-25T10:43:37.859Z,
  tagarray: [ 'zuma' ],
  views: 2000 }
{ _id: 5b30d4b22904771be030db62,
  memeid: 'eLT1F',
  url: 'http://res.cloudinary.com/memeafrica/image/upload/v1529926834/test/crying_b6fjaf.gif',
  tags: 'laugh',
  caption: '',
  imgs: 'crying.gif',
  __v: 0,
  uploadDate: 2018-06-25T11:40:34.649Z,
  tagarray: [ 'laugh' ],
  views: 0 }
]

My code:

meme.find({}, (err, meme) => {
  meme.forEach((meme) => {
    console.log(meme); 
  });
});

How do I manipulate the process so it can be array?

Thanks!

3
  • 1
    You're using Mongoose, so the meme in your meme.find callback is an array; you're iterating over it and console logging each element individually. Maybe I'm not understanding your question. Commented Jun 27, 2018 at 2:10
  • Exactly. How do I turn it into Array? Commented Jun 27, 2018 at 8:08
  • meme already is an array. Commented Jun 29, 2018 at 2:01

2 Answers 2

4

The cursor object returned by find() has a toArray() method.

meme.find().toArray((err, memes) => {
    console.log("retrieved memes:");
    console.log(memes);
});
Sign up to request clarification or add additional context in comments.

Comments

1

Just use map. It'll iterate through each meme and add them to an array.

meme.find({}, (err, meme) => {
  const memes = meme.map(m => m);
  // Use the array, pass it to a service, or pass to a callback
});

2 Comments

When I run the code it is still returning object. meme.find({}, (err, meme) => { const memes = meme.map(m => m); console.log(typeof memes); // Use the array, pass it to a service, or pass to a callback });
The commented part // Use the array, pass it to a service, or pass to a callback How do I pass to a callback? @jakeMiller

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.