【问题标题】:MongoDb, NodeJs, Express and Angular 2, Data from two collections join, retreival and displayMongoDb、NodeJs、Express 和 Angular 2,来自两个集合的数据连接、检索和显示
【发布时间】:2017-12-10 09:29:17
【问题描述】:

我有两个 mongo 集合:

  • 位置:(locationId、locationCity、locationState、locationZip 等}
  • carLocations: {carId, carType, carMake, locationId}

我想检索数据

carAvailability: {locationCity, locationState, locationZip, carMake, carType}

在我的 nodejs 层中,这是我想要做的:

首先,我尝试按 locationCity、locationState 或 locationZip 检索 locationIds 数组。一旦我有了 locationIds 数组,我就会尝试检索 carLocations。

app.get("/api/carsbylocationids", function (req, res) {
  //console.log(" locationid:" + req.query.locationid);
  var locationIds  = JSON.parse(req.query.locationIds);
  console.log("locationIds: " + locationIds);
  console.log("carId: " + req.query.carId);
  db.collection(carsLocations).find(
    {
      'locationId': {$in:locationIds},
      'carId': req.query.carId
    }
  ).toArray(function (err, docs) {
    if (err) {
      handleError(res, err.message, "Failed to get items.");
    } else {
      res.header('Access-Control-Allow-Origin', '*');
      res.header('Access-Control-Allow-Methods', 'GET);
      res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
      res.status(200).json(docs);
    }
  });

});

无论是在这部分还是 angularJS 2 代码(此处未提供),我想要 carAvailability: {locationCity, locationState, locationZip, carMake, carType} 以便我可以遍历并显示在屏幕上。

我无法找到最佳/有效的方法。我应该在 nodeJS 层或 Angular2 层中处理这种连接类型的条件吗?

【问题讨论】:

  • Http 请求是前端成本最高的操作之一。如果可以,请将它们合并到您的后端,这样您就只能从前端发出 1 个 http 请求。但当然它也取决于其他参数(可读性、复杂性等)
  • 我同意@echonax,您可以在monogDB 中使用填充功能进行此类连接。
  • 将探索 mongoDB 的“填充”特性。非常感谢......
  • 不仅仅是“填充”,在我发出 http 请求之前,我需要使用 $lookup 和聚合功能来加入。现在正在努力...
  • 最好和最简单的方法是使用填充。你可以看看它。此外,所有这些操作都可以在后端而不是前端完成。它在很多方面都非常有效。

标签: javascript node.js mongodb angular express


【解决方案1】:

我使用 $lookup 和 $match 的聚合从 mongoDb 获取数据。对于那些想要了解细节的人:

app.get("/api/locationsByitemid", function (req, res) {
  console.log(" itemid:" + req.query.itemid + " city:" + req.query.locationCity + " state:" + req.query.locationState);

  db.collection(LOCATIONS_COLLECTION).aggregate([
    { $lookup: { from: ITEMS_COLLECTION, localField: "locationId",foreignField: "locationId", as: "locationsbyLocationId"} },
    { $unwind:"$locationsbyLocationId"},
    { "$match": { 'locationsbyLocationId.itemid': req.query.itemid, 'locationCity': req.query.locationCity, 'locationState' : req.query.locationState} }
    ]
  ).toArray(function (err, docs) {
    if (err) {
      handleError(res, err.message, "Failed to get items.");
    } else {
      res.header('Access-Control-Allow-Origin', '*');
      res.header('Access-Control-Allow-Methods', 'GET');
      res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
      res.status(200).json(docs);
    }
  });

});

这令我满意,但欢迎提出任何建议......感谢那些指出最好在 RestFul 服务电话之前处理的人。这就是我所探索的。

【讨论】:

    猜你喜欢
    • 2015-07-22
    • 2013-04-26
    • 2018-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多