Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get all strings from array in mongoDb

I am a beginner in node and mongoDb. I send a models from the application to node.

const { users } = req.body;

console.log(users);

and when I print this I have this result.

[
  '{"id":"6389f4206f784a8e3d75cf10","email":"test5@test.com","password":"$2a$08$2cE5XFlShV0nBYS6JF4ckuRLcEvp75YuCazQLSyIZ6bKu5kev/dwS","verify":1,"invitationsToMe":["6390a3b8cb716fa4d67d82d6"],"invitationsFromMe":[],"friendsList":["638777339b32b61a57e5ab4b","638889b46b30846785db55df","638776e89b32b61a57e5ab43","638797306307ba909c43be22"],"username":"Przemek"}',
  '{"id":"638797306307ba909c43be22","email":"test3@test.com","password":"$2a$08$wvtwkdwzKg.ZbEvWb2L5desAwoX0gUj723V/WgqiRRRFGl4AEltrm","verify":1,"invitationsToMe":["6390a3b8cb716fa4d67d82d6"],"invitationsFromMe":[],"friendsList":["638777339b32b61a57e5ab4b","638776e89b32b61a57e5ab43","638889b46b30846785db55df","6389f4206f784a8e3d75cf10"],"username":"Maciek"}',
  '{"id":"638777339b32b61a57e5ab4b","email":"test2@test.com","password":"$2a$08$Zl8eJP8B2yYJaFMdH5045uQZnKB8JMmOMmMfCQXN/C0UfN1eMWdAi","verify":1,"invitationsToMe":["6390a3b8cb716fa4d67d82d6"],"invitationsFromMe":[],"friendsList":["638797306307ba909c43be22","638776e89b32b61a57e5ab43","638889b46b30846785db55df","6389f4206f784a8e3d75cf10"],"username":"Bartolinio"}'
]

Now i want to extract from each "id" number to save in array in mondoDb. I found only .find( with filters ) but it not working for me.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

const strings = [
  '{"id":"6389f4206f784a8e3d75cf10","email":"test5@test.com","password":"$2a$08$2cE5XFlShV0nBYS6JF4ckuRLcEvp75YuCazQLSyIZ6bKu5kev/dwS","verify":1,"invitationsToMe":["6390a3b8cb716fa4d67d82d6"],"invitationsFromMe":[],"friendsList":["638777339b32b61a57e5ab4b","638889b46b30846785db55df","638776e89b32b61a57e5ab43","638797306307ba909c43be22"],"username":"Przemek"}',
  '{"id":"638797306307ba909c43be22","email":"test3@test.com","password":"$2a$08$wvtwkdwzKg.ZbEvWb2L5desAwoX0gUj723V/WgqiRRRFGl4AEltrm","verify":1,"invitationsToMe":["6390a3b8cb716fa4d67d82d6"],"invitationsFromMe":[],"friendsList":["638777339b32b61a57e5ab4b","638776e89b32b61a57e5ab43","638889b46b30846785db55df","6389f4206f784a8e3d75cf10"],"username":"Maciek"}',
  '{"id":"638777339b32b61a57e5ab4b","email":"test2@test.com","password":"$2a$08$Zl8eJP8B2yYJaFMdH5045uQZnKB8JMmOMmMfCQXN/C0UfN1eMWdAi","verify":1,"invitationsToMe":["6390a3b8cb716fa4d67d82d6"],"invitationsFromMe":[],"friendsList":["638797306307ba909c43be22","638776e89b32b61a57e5ab43","638889b46b30846785db55df","6389f4206f784a8e3d75cf10"],"username":"Bartolinio"}'
]

const objects = strings.map(str => JSON.parse(str))

console.log(objects)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading