我如何在 Mongoose 中优化以下查询?
How can i optimize below query in Mongoose?
基本上,我想从集合中获取数据,其中 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,之后我得到带有输出和空值的数组,所以我使用 filter 只获取 pincode
虽然我得到了我想要的输出,但我仍然不确定它是否是一种优化方法。
如果我对您的 JS 代码的理解正确,您只想将符合条件的值放入 location
数组。
在这种情况下,这是一种在查询中执行所有操作的更好方法。您可以使用聚合管道。
这个查询:
vendorStatus: "Active
首次匹配您的 find
查询。
- 然后使用
$unwind
解构数组并查看每个值。
$match
查找 pincode
和 isLive
所需的值。
- 最后一个阶段是
$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
基本上,我想从集合中获取数据,其中 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,之后我得到带有输出和空值的数组,所以我使用 filter 只获取 pincode 虽然我得到了我想要的输出,但我仍然不确定它是否是一种优化方法。
如果我对您的 JS 代码的理解正确,您只想将符合条件的值放入 location
数组。
在这种情况下,这是一种在查询中执行所有操作的更好方法。您可以使用聚合管道。
这个查询:
vendorStatus: "Active
首次匹配您的find
查询。- 然后使用
$unwind
解构数组并查看每个值。 $match
查找pincode
和isLive
所需的值。- 最后一个阶段是
$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