Find and save inside for loop in mongodb

Question

I have my request having a array of contacts i have to fine whether the contact is in the database:

{
    "vishReqHeader": {
        "ClientId": "0",
        "AppId": "101",
        "ApiCode": "1007",
        "ReqNo": "123456789"
    },
    "vishReqBody": {
        "RegPhNo": "9880889660",
        "Cnt": "3",
        "Contacts": [{
                "PhNo": "9880889660",
                "Name": "Alex"
            },
            {
                "PhNo": "9538607847",
                "Name": "Cindy"
            },
            {
                "PhNo": "9886949651",
                "Name": "Rob"
            }
        ]

    },
    "vishReqTrailer": {
        "DeviceData": "lat=12.9677992|lng=77.596204|IMEI=1234567890|appVer=3.0.1|OS=ANDROID|SoftVer=6.0.1",
        "Authentication": "securitytoken"
    }
}

If present fetch the details and save the details by adding the fetched details.
If not just add the contact in the database, but the query is not working as expected

router.route('/uploadContacts')
    .post(function(req,res){
        var j;
        for( j = 0; j < req.body.vishReqBody.Contacts.length; j++) {
            ProfileDetails.findOne({ 'PhNo' :  req.body.vishReqBody.Contacts[j].PhNo} ,
                function(err, profileDetail) {
                    console.log("*************reqreq********************")

                    console.log(j)
                if(profileDetail && profileDetail.PhNo ){

                    console.log(req.body.vishReqBody.Contacts)

                    console.log("*************reqreq********************")
                    var contactsDetails = new contacts();
                    contactsDetails.PhNo=req.body.vishReqBody.Contacts[j].PhNo
                    contactsDetails.RegStatus=profileDetail.Reg_Status
                    contactsDetails.profileId=profileDetail.Profile_Id
                    contactsDetails.PhoneBookName=req.body.vishReqBody.Contacts[j].Name
                    contactsDetails.updatedByProfileId=req.body.vishReqHeader.AppId

                    contactsDetails.save(function(err,result){

                    })
                }

                });

        }
})

here in the code

If i do find it is displaying the find details properly , when i try to save the jth value in the for loop is always 3

Not sure of the async call that is passing through

please help me to correct the quire , every element in the contacts array has to do find and save how i do it


Show source
| arrays   | find   | save   | for-loop   | mongodb   2017-10-06 07:10 1 Answers

Answers to Find and save inside for loop in mongodb ( 1 )

  1. 2017-10-06 07:10

    No need to fire a query for each phone number

    First get all of the phone numbers in an array.

    var phoneNumberArray =req.vishReqBody.Contacts.map(function(contact){
        return contact.PhNo
    })
    

    Then use the following code:

    ProfileDetails.findOne({ 'PhNo' : {$in:phoneNumberArray}})
    

    To get the name of the matched contact from your request,

    req.vishReqBody.Contacts.find(function(contact){
        return contact.PhNo == profileDetail.PhNo
    })
    

Leave a reply to - Find and save inside for loop in mongodb

◀ Go back