简体   繁体   English

如何在Node JS中从Mongodb查询两个集合

[英]How to query two collections from Mongodb in node js

I have VisitModel that has saved the visit of various users to different visiter locations. 我有VisitModel,它已将各种用户的访问保存到不同的访问者位置。

{ 
    "_id" : ObjectId("5bb5d748b8c8a07fee6b4437"), 
    "__v" : NumberInt(0), 
    "ctopamount" : NumberInt(500), 
    "hrno" : NumberInt(200900410), 
    "lastUpdationTime" : ISODate("2018-10-04T09:03:04.619+0000"), 
    "mobileNo" : "9413394574", 
    "name" : "Hirdesh Sharma", 
    "simInventory" : NumberInt(10), 
    "ssa" : "cojp", 
    "visitedRetailer" : "7597000662", 
    "visitLocation" : {
        "lat" : "26.9140156", 
        "lng" : "75.7997062"
    }
}
// ----------------------------------------------
{ 
    "_id" : ObjectId("5bb5ea682b955e0961e55b24"), 
    "__v" : NumberInt(0), 
    "ctopamount" : NumberInt(10), 
    "hrno" : NumberInt(200900410), 
    "lastUpdationTime" : ISODate("2018-10-04T10:24:40.180+0000"), 
    "mobileNo" : "9413394574", 
    "name" : "Hirdesh Sharma", 
    "simInventory" : NumberInt(10), 
    "ssa" : "cojp", 
    "visitedRetailer" : "7597000662", 
    "visitLocation" : {
        "lat" : "26.9140203", 
        "lng" : "75.7997103"
    }
}
// ----------------------------------------------
{ 
    "_id" : ObjectId("5bb71766edeebb1682902c5a"), 
    "__v" : NumberInt(0), 
    "ctopamount" : NumberInt(0), 
    "hrno" : NumberInt(200900410), 
    "lastUpdationTime" : ISODate("2018-10-05T07:48:54.566+0000"), 
    "mobileNo" : "9413394574", 
    "name" : "Hirdesh Sharma", 
    "simInventory" : NumberInt(0), 
    "ssa" : "cojp", 
    "visitedRetailer" : "123456789", 
    "visitLocation" : {
        "lat" : "26.913970499999998", 
        "lng" : "75.799515"
    }
}
// ----------------------------------------------
{ 
    "_id" : ObjectId("5bb71991edeebb1682902c5b"), 
    "__v" : NumberInt(0), 
    "ctopamount" : NumberInt(0), 
    "hrno" : NumberInt(200900410), 
    "lastUpdationTime" : ISODate("2018-10-05T07:58:09.241+0000"), 
    "mobileNo" : "9413394574", 
    "name" : "Hirdesh Sharma", 
    "simInventory" : NumberInt(0), 
    "ssa" : "cojp", 
    "visitedRetailer" : "1234567890", 
    "visitLocation" : {
        "lat" : "26.9140163", 
        "lng" : "75.7997024"
    }
}
// ----------------------------------------------
{ 
    "_id" : ObjectId("5bb72c28edeebb1682902c5d"), 
    "__v" : NumberInt(0), 
    "ctopamount" : NumberInt(10000), 
    "hrno" : NumberInt(200901869), 
    "lastUpdationTime" : ISODate("2018-10-05T09:17:28.825+0000"), 
    "mobileNo" : "9413395846", 
    "name" : "Dharmendra Soni", 
    "simInventory" : NumberInt(15), 
    "ssa" : "Circle Office", 
    "visitedRetailer" : "0000000000", 
    "visitLocation" : {
        "lat" : "26.8922714", 
        "lng" : "75.8042042"
    }
}


// ----------------------------------------------
{ 
    "_id" : ObjectId("5bb72e50edeebb1682902c5e"), 
    "__v" : NumberInt(0), 
    "ctopamount" : NumberInt(5000), 
    "hrno" : NumberInt(200901869), 
    "lastUpdationTime" : ISODate("2018-10-05T09:26:40.371+0000"), 
    "mobileNo" : "9413395846", 
    "name" : "Dharmendra Soni", 
    "simInventory" : NumberInt(10), 
    "ssa" : "Circle Office", 
    "visitedRetailer" : "9414002070", 
    "visitLocation" : {
        "lat" : "26.8922503", 
        "lng" : "75.8041773"
    }
}
// ----------------------------------------------
{ 
    "_id" : ObjectId("5bb72f49edeebb1682902c5f"), 
    "__v" : NumberInt(0), 
    "ctopamount" : 1.5, 
    "hrno" : NumberInt(200901869), 
    "lastUpdationTime" : ISODate("2018-10-05T09:30:49.843+0000"), 
    "mobileNo" : "9413395846", 
    "name" : "Dharmendra Soni", 
    "simInventory" : null, 
    "ssa" : "Circle Office", 
    "visitedRetailer" : "7597000559", 
    "visitLocation" : {
        "lat" : "26.8922608", 
        "lng" : "75.8041638"
    }
}
// ----------------------------------------------
{ 
    "_id" : ObjectId("5bb759e7edeebb1682902c63"), 
    "__v" : NumberInt(0), 
    "ctopamount" : NumberInt(0), 
    "hrno" : NumberInt(198911286), 
    "lastUpdationTime" : ISODate("2018-10-05T12:32:39.394+0000"), 
    "mobileNo" : "9413395614", 
    "name" : "Shankara lal meena", 
    "simInventory" : NumberInt(0), 
    "ssa" : "Chittorgarh", 
    "visitedRetailer" : "7597000559", 
    "visitLocation" : {
        "lat" : "24.038592", 
        "lng" : "74.7767928"
    }
}
}

i have another model which has a saved database of RetailerLocation which has saved the filed retailer name ,address ,retailer number and a location 我有另一个模型,该模型具有一个已保存的RetailerLocation数据库,该数据库已保存归档的零售商名称,地址,零售商编号和位置

{ 
    "_id" : ObjectId("5c864a80498ceb2d94a8a029"), 
    "Name of Retailer" : "Karan Singh", 
    "SSA" : "JAIPUR", 
    "savedLocation" : {
        "lat" : 26.8122376, 
        "lng" : 75.7783984
    }, 
     "Retailernumber"  : "7597000559"
}
// ----------------------------------------------
{ 
    "_id" : ObjectId("5c864a80498ceb2d94a8a02a"), 
    "Name of Retailer" : "HANUMAN PRASAD", 
    "SSA" : "JAIPUR", 
    "savedLocation" : {
        "lat" : 26.9142872, 
        "lng" : 75.7431824
    }, 
     "Retailernumber"  : "7597000632"
}
// ----------------------------------------------
{ 
    "_id" : ObjectId("5c864a80498ceb2d94a8a02b"), 
    "Name of Retailer" : "Karan Singh", 
    "SSA" : "JAIPUR", 
    "savedLocation" : {
        "lat" : 26.9039945, 
        "lng" : 75.8325143
    }, 
     "Retailernumber"  : "7597000633"
}
// ----------------------------------------------
{ 
    "_id" : ObjectId("5c864a80498ceb2d94a8a02c"), 
    "Name of Retailer" : "kunal hemani", 
    "SSA" : "JAIPUR", 
    "savedLocation" : {
        "lat" : 26.931319, 
        "lng" : 75.8201112
    }, 
    "Retailernumber" : "7597000638"
}
// ----------------------------------------------
{ 
    "_id" : ObjectId("5c864a80498ceb2d94a8a02d"), 
    "Name of Retailer" : "kumawat floor mill", 
    "SSA" : "JAIPUR", 
    "savedLocation" : {
        "lat" : 26.8592253, 
        "lng" : 75.7851659
    }, 
    "Retailernumber": "7597000662"
}
// ----------------------------------------------
{ 
    "_id" : ObjectId("5c864a80498ceb2d94a8a02e"), 
    "Name of Retailer" : "care point", 
    "SSA" : "JAIPUR", 
    "savedLocation" : {
        "lat" : 26.9247296, 
        "lng" : 75.7894896
    }, 
    "Retailernumber" : "7597000708"
}
// ----------------------------------------------
{ 
    "_id" : ObjectId("5c864a80498ceb2d94a8a02f"), 
    "Name of Retailer" : "SAYED MOBILE", 
    "SSA" : "JAIPUR", 
    "savedLocation" : {
        "lat" : 26.933338, 
        "lng" : 75.8074372
    }, 
    "Retailernumber" : "7597000718"
}
// ----------------------------------------------
{ 
    "_id" : ObjectId("5c864a80498ceb2d94a8a030"), 
    "Name of Retailer" : "jaipur moters", 
    "SSA" : "JAIPUR", 
    "savedLocation" : {
        "lat" : 26.9187819, 
        "lng" : 75.8102266
    }, 
    "Retailernumber" : "7597000773"
}
// ----------------------------------------------
{ 
    "_id" : ObjectId("5c864a80498ceb2d94a8a031"), 
    "Name of Retailer" : "sona telecom", 
    "SSA" : "JAIPUR", 
    "savedLocation" : {
        "lat" : 26.9212756, 
        "lng" : 75.8085795
    }, 
    "Retailernumber" : "7597000776"
}
// ----------------------------------------------
{ 
    "_id" : ObjectId("5c864a80498ceb2d94a8a032"), 
    "Name of Retailer" : "sangam mobile", 
    "SSA" : "JAIPUR", 
   "savedLocation" : {
        "lat" : 26.9194859, 
        "lng" : 75.8078163
    }, 
    "Retailernumber" : "7597000785"
}
// ----------------------------------------------
{ 
    "_id" : ObjectId("5c864a80498ceb2d94a8a033"), 
    "Name of Retailer" : "bombay  collection", 
    "SSA" : "JAIPUR", 
   "savedLocation" : {
        "lat" : 26.8564543, 
        "lng" : 75.8207683
    }, 
   "Retailernumber": "7597000792"
}

now i want to display the data as following 现在我想显示数据如下

{
    "ctopamount" : NumberInt(500), 
    "hrno" : NumberInt(200900410), 
    "mobileNo" : "9413394574", 
    "name" : "Hirdesh Sharma", 
    "simInventory" : NumberInt(10), 
    "ssa" : "cojp", 
    "visitedRetailer" : "7597000662", 
    "visitLocation" : {
        "lat" : "26.9140156", 
        "lng" : "75.7997062"
    }
  "savedLocation" : {
        "lat" : "26.9140156", 
        "lng" : "75.7997062"
    }
}

where the saved location should be fetched from RetailerLocation collection based on visitedRetailer=RetailerMobile 应当根据visitedRetailer=RetailerMobile从RetailerLocation集合中获取保存的位置

In your visit model instead of saving the visit location, you can directly save the object id of the retailers from the retailer location. 在访问模型中,您可以直接从零售商位置保存零售商的对象ID,而不必保存访问位置。 Then you can just use the populate the data. 然后,您可以使用填充数据。 Here is the reference to the populate in Mongoose 这是对猫鼬人口的参考

I hope this helps. 我希望这有帮助。

Try with this. 试试这个。

Write aggregate function in visitModel and lookup to RetailerLocation. 在visitModel中编写聚合函数并查找到RetailerLocation。

Test1 -> visitModel Test2 ->RetailerLocation Test1-> visitModel Test2-> RetailerLocation

db.getCollection('Test1').aggregate([
{
$lookup:{
    from:"Test2",
    localField:"visitedRetailer",
    foreignField:"Retailernumber",
    as:"retailerDetails"
    }
},
{
$unwind:{
    path:"$retailerDetails",
    preserveNullAndEmptyArrays:true
    }
},
{
$project:{
            "ctopamount" :1, 
            "hrno" : 1, 
            "mobileNo" : 1, 
            "name" : 1, 
            "simInventory" :1, 
            "ssa" : 1, 
            "visitedRetailer" : 1, 
            "visitLocation" : 1,
            "savedLocation" : "$retailerDetails.savedLocation"
    }
}
])

I don't have access to comment but with respect to @Ashwanth Madhav 's answer, you can do the same in mongoose. 我无权发表评论,但对于@Ashwanth Madhav的回答,您可以使用猫鼬做同样的事情。

Test1.aggregate([{
$lookup:{
from:"Test2",
localField:"visitedRetailer",
foreignField:"Retailernumber",
as:"retailerDetails"
}
},
{
$unwind:{
path:"$retailerDetails",
preserveNullAndEmptyArrays:true
}
},
{
$project:{
        "ctopamount" :1, 
        "hrno" : 1, 
        "mobileNo" : 1, 
        "name" : 1, 
        "simInventory" :1, 
        "ssa" : 1, 
        "visitedRetailer" : 1, 
        "visitLocation" : 1,
        "savedLocation" : "$retailerDetails.savedLocation"
}}
                ]).exec(function (err,fetcheddata) {

    if( _.isNull(err) && fetcheddata.length > 0 ){
        var response = genRes.generateResponse(true,"found successfully");
        callback(response,fetcheddata);
    }
    else if( fetcheddata==undefined || transactions.length == 0 ){
        var response = genRes.generateResponse(false,"No Data found");
        callback(response,null);
    }
    else{
        var response = genRes.generateResponse(false,"there occurred some error : "+err);
        callback(response,null)
    }
})
};

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

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