简体   繁体   English

在mongodb中查找并保存for循环

[英]Find and save inside for loop in mongodb

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 如果我确实找到它,则会正确显示查找详细信息,当我尝试将第j个值保存在for循环中时,它总是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 请帮助我纠正quire,contacts数组中的每个元素都必须查找并保存我的操作方式

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
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM