【问题标题】:How to query two collections from Mongodb in node js如何在节点 js 中从 Mongodb 查询两个集合
【发布时间】:2019-08-03 22:12:40
【问题描述】:

我有 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集合中获取保存的位置

【问题讨论】:

    标签: node.js mongodb mongoose find aggregate


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      试试这个。

      在 visitModel 中编写聚合函数并查找 RetailerLocation。

      Test1 -> 访问模型 Test2 ->零售商位置

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

      【讨论】:

      • 感谢 madhav 这行得通,但是我们可以在 node.js 中使用 mongoose 吗
      • 对不起兄弟。我对猫鼬不熟悉。
      【解决方案3】:

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

      我希望这会有所帮助。

      【讨论】:

      • 现在我无法更改文档设计。请使用现有集合的任何解决方案。访问模型中的另一件事是,保存的位置是从移动 gps 获取的,而位置表格零售商位置是参考位置。这样我就可以比较这两个位置的距离。感谢您的及时回复
      猜你喜欢
      • 2015-10-07
      • 2019-08-02
      • 2019-10-21
      • 1970-01-01
      • 2016-09-27
      • 1970-01-01
      • 2020-09-19
      • 1970-01-01
      • 2021-11-22
      相关资源
      最近更新 更多