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!
memein yourmeme.findcallback is an array; you're iterating over it and console logging each element individually. Maybe I'm not understanding your question.memealready is an array.