Home > other >  Get all strings from array in mongoDb
Get all strings from array in mongoDb

Time:12-11

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":"[email protected]","password":"$2a$08$2cE5XFlShV0nBYS6JF4ckuRLcEvp75YuCazQLSyIZ6bKu5kev/dwS","verify":1,"invitationsToMe":["6390a3b8cb716fa4d67d82d6"],"invitationsFromMe":[],"friendsList":["638777339b32b61a57e5ab4b","638889b46b30846785db55df","638776e89b32b61a57e5ab43","638797306307ba909c43be22"],"username":"Przemek"}',
  '{"id":"638797306307ba909c43be22","email":"[email protected]","password":"$2a$08$wvtwkdwzKg.ZbEvWb2L5desAwoX0gUj723V/WgqiRRRFGl4AEltrm","verify":1,"invitationsToMe":["6390a3b8cb716fa4d67d82d6"],"invitationsFromMe":[],"friendsList":["638777339b32b61a57e5ab4b","638776e89b32b61a57e5ab43","638889b46b30846785db55df","6389f4206f784a8e3d75cf10"],"username":"Maciek"}',
  '{"id":"638777339b32b61a57e5ab4b","email":"[email protected]","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.

CodePudding user response:

const strings = [
  '{"id":"6389f4206f784a8e3d75cf10","email":"[email protected]","password":"$2a$08$2cE5XFlShV0nBYS6JF4ckuRLcEvp75YuCazQLSyIZ6bKu5kev/dwS","verify":1,"invitationsToMe":["6390a3b8cb716fa4d67d82d6"],"invitationsFromMe":[],"friendsList":["638777339b32b61a57e5ab4b","638889b46b30846785db55df","638776e89b32b61a57e5ab43","638797306307ba909c43be22"],"username":"Przemek"}',
  '{"id":"638797306307ba909c43be22","email":"[email protected]","password":"$2a$08$wvtwkdwzKg.ZbEvWb2L5desAwoX0gUj723V/WgqiRRRFGl4AEltrm","verify":1,"invitationsToMe":["6390a3b8cb716fa4d67d82d6"],"invitationsFromMe":[],"friendsList":["638777339b32b61a57e5ab4b","638776e89b32b61a57e5ab43","638889b46b30846785db55df","6389f4206f784a8e3d75cf10"],"username":"Maciek"}',
  '{"id":"638777339b32b61a57e5ab4b","email":"[email protected]","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)

  • Related