我目前正在与MongoDB中的Aggregate框架作斗争,试图找到我想要的结果。我有一个正确提取子文档的查询,但我现在想根据子文档中的条件过滤这个并进一步排序(这就是为什么我不能使用Find())
我目前将此作为我的查询,但$ if语句不能正常工作
const regions = await Region.aggregate(
{ $match: { name: res.locals.currentUser.region } },
{ $lookup: { from: 'products', localField: 'products', foreignField: '_id', as: 'returned_product' } },
{
"$redact": {
"$cond": {
"if": {
$eq: ["$category", "motors"] },
"then": "$$DESCEND",
"else": "$$PRUNE"
}
}
},
{ $sort: { 'product.urn': 1 } });
res.json(regions);
如果没有Redact,我可以根据我的初始过滤器成功提取子文档,这将以下列格式返回:
[
{
"_id": "5a3010380a927941bf38ba87",
"__v": 0,
"name": "glasgow",
"products": [
"5a2fb8582d8b90614150d53c",
"5a2fb8a02d8b90614150d53d",
"5a301d16d45fb65efd513e63",
"5a30212c04078b6aa043bcc0"
],
"returned_product": [
{
"_id": "5a2fb8582d8b90614150d53c",
"urn": "le00005",
"category": "motors",
"thumbnail": "https://storage.googleapis.com/thecreativestore/creativestore/leisure/LE00001/ceebdbed17cc99d12d327275ac50e24e-thumb",
"image": "https://storage.googleapis.com/thecreativestore/creativestore/leisure/LE00001/ceebdbed17cc99d12d327275ac50e24e-full",
"printProof": "https://storage.googleapis.com/thecreativestore/1513076823969LE00001.pdf",
"digitalProof": "https://storage.googleapis.com/thecreativestore/1513076823978MO00002ASAS.pdf",
"__v": 0,
"tags": [
"business"
],
"mockups": [
"https://storage.googleapis.com/thecreativestore/creativestore/leisure/LE00001/d7a4a45b00a76648e0778f571746cc21-full",
"https://storage.googleapis.com/thecreativestore/creativestore/leisure/LE00001/c7e029d91eb63ba1092e092d0a9b897c-full",
"https://storage.googleapis.com/thecreativestore/creativestore/leisure/LE00001/cae3859f497fc4b9b63425d6b7324bd4-full",
"https://storage.googleapis.com/thecreativestore/creativestore/leisure/LE00001/de53bd7dec272322ce7e517af5309736-full",
"https://storage.googleapis.com/thecreativestore/creativestore/leisure/LE00001/c5321e3e94e311d9eba6014991c40a83-full"
]
},
{
"_id": "5a301d16d45fb65efd513e63",
"urn": "motors2",
"category": "motors",
"thumbnail": "https://storage.googleapis.com/thecreativestore/creativestore/motors/motors2/5eec4730ad7f5a7c79cca286dc6ffb27-thumb",
"image": "https://storage.googleapis.com/thecreativestore/creativestore/motors/motors2/5eec4730ad7f5a7c79cca286dc6ffb27-full",
"printProof": "https://storage.googleapis.com/thecreativestore/1513102613401Digital File Template.pdf",
"digitalProof": "https://storage.googleapis.com/thecreativestore/1513102613408Digital File Template.pdf",
"tags": [
"motors"
],
"mockups": [
"https://storage.googleapis.com/thecreativestore/creativestore/motors/motors2/e249adfba22d553af7f87fda48dcf29a-full",
"https://storage.googleapis.com/thecreativestore/creativestore/motors/motors2/67f572530cf1875f547b6bc88d8c259a-full"
],
"__v": 0
},
{
"_id": "5a30212c04078b6aa043bcc0",
"urn": "sadsadf",
"category": "leisure",
"thumbnail": "https://storage.googleapis.com/thecreativestore/creativestore/leisure/sadsadf/1ed6f624d7e99fc90440e77547555185-thumb",
"image": "https://storage.googleapis.com/thecreativestore/creativestore/leisure/sadsadf/1ed6f624d7e99fc90440e77547555185-full",
"printProof": "https://storage.googleapis.com/thecreativestore/1513103659119Digital File Template.pdf",
"digitalProof": "https://storage.googleapis.com/thecreativestore/1513103659127Digital File Template.pdf",
"tags": [
"leisure"
],
"mockups": [
"https://storage.googleapis.com/thecreativestore/creativestore/leisure/sadsadf/c9e6e465b38b7c2242171ab341ca46ec-full"
],
"__v": 0
}
]
我一直在用这种方式敲打头(或以某种方式将数据分类为这种格式),所以即使是一些指针也会很棒!
干杯,