如何在节点js中从Mongodb查询两个集合

时间:2019-03-13 09:32:25

标签: node.js mongodb mongoose find aggregate

我有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"
    }
}
}

我有另一个模型,该模型具有保存的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"
}

现在我要显示以下数据

{
    "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"
    }
}

应基于visitedRetailer=RetailerMobile

从RetailerLocation集合中获取保存的位置

3 个答案:

答案 0 :(得分:0)

在访问模型中,您可以直接从零售商位置保存零售商的对象ID,而不必保存访问位置。然后,您可以使用填充数据。这是对Mongoose

中的填充的引用

我希望这会有所帮助。

答案 1 :(得分:0)

尝试一下。

在visitModel中写聚合函数并查找到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"
    }
}
])

答案 2 :(得分:0)

我无权发表评论,但是对于@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)
    }
})
};