【问题标题】:How can i optimize below query in Mongoose?如何在 Mongoose 中优化以下查询?
【发布时间】:2021-11-19 04:22:00
【问题描述】:

基本上,我想从其中 pincode 字段(在 Location 数组中)与 req.body 匹配的集合中获取数据 我得到了我想要的输出,但我不确定它是否是一种优化的方式(查看 Mongoose 查询部分)

这是我的 Json:

{
    "_id" : ObjectId("6115b2cc1681596a10072f97"),
    "vendorStatus" : "Active",
    "name" : "harshit singh bhutani",
    "email" : "sharsh2106@gmail.com",
    "vId" : 12121,
    "contact" : 121212,
    "cities" : "dell",
    "isMidnight" : "NA",
    "location" : [ 
        {
            "delivery" : 1266,
            "dc" : "High",
            "midnight" : "Active",
            "isLive" : "NA",
            "_id" : ObjectId("612433c27292d11154bc4d4d"),
            "pincode" : 123100,
            "city" : "dek"
        }, 
        {
            "delivery" : 23,
            "dc" : "High",
            "midnight" : "Active",
            "isLive" : "NA",
            "_id" : ObjectId("612441473cb5766a2457d6db"),
            "pincode" : 1212,
            "city" : "dd"
        }
    ],
    
    "createdAt" : ISODate("2021-08-12T23:46:20.407Z"),
    "updatedAt" : ISODate("2021-09-03T10:51:34.756Z"),
    "__v" : 73
}

这是我在 Mongoose 中的查询:

  const { pin } = req.body;
  const vendor = await Vendor.find({ vendorStatus: "Active" });

  const pincode = vendor.map((item) => {
    return item.location.find((i) => {
      if (i.pincode === Number(pin) && i.isLive === "Active") {
        return i;
      }
    });
  });

  const pincodefound = pincode.filter(Boolean);
  if (pincodefound.length === 0) {
    res.status(404);
    throw Error("Product is not Deliverable at this Pincode");
  } else {
    return res.json({
      pincodefound,
    });
  }

首先我使用 map 进行迭代,然后我使用 find 来获取匹配的 pincode,之后我得到带有输出和 null 值的数组,所以我使用 filter 来只获取 pincode 虽然我得到了我想要的输出但我仍然不确定它是否是一种优化的方法。

【问题讨论】:

    标签: javascript node.js mongodb mongoose mern


    【解决方案1】:

    如果我正确理解了您的 JS 代码,您只想将值放入与条件匹配的 location 数组中。

    在这种情况下,这是在查询中执行所有操作的更好方法。您可以使用聚合管道。

    这个查询:

    • vendorStatus: "Active 的第一个匹配项就像您的 find 查询一样。
    • 然后使用$unwind 解构数组并查看每个值。
    • $match 查找 pincodeisLive 是所需值的值。
    • 最后一个阶段是$project,仅显示您想要的值,在本例中为位置值。
    const pincode = await Vendor.aggregate([
      {
        "$match": {
          "vendorStatus": "Active"
        }
      },
      {
        "$unwind": "$location"
      },
      {
        "$match": {
          "location.pincode": pin,
          "location.isLive": "Active"
        }
      },
      {
        "$project": {
          "delivery": "$location.delivery",
          "dc": "$location.dc",
          "midnight": "$location.midnight",
          "isLive": "$location.isLive",
          "_id": "$location._id",
          "pincode": "$location.pincode",
          "city": "$location.city"
        }
      }
    ]);
    

    例如here

    【讨论】:

    • 嗨,伙计,感谢您的精彩解释和代码,它真的很有帮助
    • 你可以像this一样使用$limit: 1。或者进elsepincode[0]
    猜你喜欢
    • 2012-08-21
    • 1970-01-01
    • 1970-01-01
    • 2020-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-04
    相关资源
    最近更新 更多