[英]mongoose find query to perform multiple condition
I have two tables vendor and vendorOrg table.我有两个表 vendor 和 vendorOrg 表。 I needs to return the list based on vendorOrg table criteria wise and category -wise我需要根据 vendorOrg 表标准和类别返回列表
const vendors = [{
"name" : "Alfred",
"location" : "FH",
"vendorOrgId" : "1"
},
{
"name" : "Alfred",
"location" : "ADH",
"vendorOrgId" : "2"
},
{
"name" : "Alfred",
"location" : "AFF",
"vendorOrgId" : "41"
}]
const vendorOrg = [
{
"orgName" : "star super market",
"vendorOrgId" : "1",
"category" : "grocery",
"status" : "active"
},
{
"orgName" : "L.f super market",
"vendorOrgId" : "41",
"category" : "grocery",
"status" : "active"
},
{
"orgName" : "Fresh mart",
"vendorOrgId" : "2",
"category" : "Milk",
"status" : "active"
}
]
find conditions below, 1.vendor's table vendorOrgId must be same as vendorOrg's table id.下面找条件,1.vendor的表vendorOrgId必须和vendorOrg的表id一样。 2. vendorOrg table status should active. 2. vendorOrg 表状态应该是活跃的。 if above conditions are true, the i needs a vendor list category-wise, based on vendorOrg's table category.如果上述条件成立,我需要一个基于 vendorOrg 表类别的供应商列表类别。
{
"grocery": [{
"name": "Alfred",
"location": "FH",
"vendorOrgId": "1"
},
{
"name": "Alfred",
"location": "AFF",
"vendorOrgId": "41"
}
],
"milk": [{
"name": "Alfred",
"location": "ADH",
"vendorOrgId": "2"
}]
}
Is this possible to do with mongoose. Thanks!这可能与 mongoose 相关吗?谢谢!
$lookup
with vendorLog collection and pass vendorOrgId
to lookup pipeline $lookup
with vendorLog 集合并将vendorOrgId
传递给查找管道$match
required conditions $match
所需条件$project
to show required fields $project
显示必填字段$unwind
deconstruct org
array $unwind
解构org
数组$group
by org category
and construct array of vendors
$group
按org category
分组并构建vendors
数组$group
by null and construct array in key-value format $group
by null 构造key-value格式的数组$arrayToObject
convert key-value pair array to object $arrayToObject
将键值对数组转换为 object$replaceRoot
to replace object to root $replaceRoot
将 object 替换为 rootdb.vendors.aggregate([
{
$lookup: {
from: "vendorOrg",
let: { vendorOrgId: "$vendorOrgId" },
pipeline: [
{
$match: {
$expr: { $eq: ["$$vendorOrgId", "$vendorOrgId"] },
status: "active"
}
},
{ $project: { _id: 1, category: 1 } }
],
as: "org"
}
},
{ $unwind: "$org" },
{
$group: {
_id: "$org.category",
vendors: {
$push: {
name: "$name",
location: "$location",
vendorOrgId: "$vendorOrgId"
}
}
}
},
{
$group: {
_id: null,
vendors: { $push: { k: "$_id", v: "$vendors" } }
}
},
{ $replaceRoot: { newRoot: { $arrayToObject: "$vendors" } } }
])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.