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: '.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: '.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: '.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: '.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: '.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: '.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!
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./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./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./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./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./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./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!
Share Improve this question asked Jun 26, 2018 at 23:29 Counter JCounter J 1831 gold badge4 silver badges17 bronze badges 3-
1
You're using Mongoose, so the
meme
in yourmeme.find
callback is an array; you're iterating over it and console logging each element individually. Maybe I'm not understanding your question. – JohnnyHK Commented Jun 27, 2018 at 2:10 - Exactly. How do I turn it into Array? – Counter J Commented Jun 27, 2018 at 8:08
-
meme
already is an array. – JohnnyHK Commented Jun 29, 2018 at 2:01
2 Answers
Reset to default 4The cursor object returned by find()
has a toArray()
method.
meme.find().toArray((err, memes) => {
console.log("retrieved memes:");
console.log(memes);
});
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
});