我有这样的文件:
{
"_id" : ObjectId("5c763335eabaf70627249465"),
"permissions" : [
"adminCompany"
],
"city" : "890",
"companyName" : "qwe",
"country" : "890",
"county" : "890",
"email" : "varadi83gabor@gmail.com",
"headquarters" : null,
"modifyed" : null,
"name" : "qwe",
"nickname" : null,
"password" : "202cb962ac59075b964b07152d234b70",
"postalCode" : "890",
"registered" : ISODate("2019-02-27T06:50:29.035Z"),
"shops" : [
{
"_id" : ObjectId("5c797074b2e01c38ac55b796"),
"city" : "Nagymaros",
"country" : "Hungary",
"county" : "Pesyt",
"currency" : "HUF",
"district" : null,
"language" : null,
"name" : "Piknik",
"phone" : "123123123",
"places" : [
{
"_id" : ObjectId("5c79714fb2e01c2ee3196a63"),
"name" : "terasz",
"products" : [
{
"_id" : ObjectId("5c797162b2e01c2ee3196a65"),
"active" : true,
"added" : null,
"description" : "borocska baszod",
"modified" : null,
"name" : "bor",
"taxPercentage" : 27,
"unit" : "liter",
"unitPrice" : 1500
}
],
"qrcodes" : [
{
"_id" : ObjectId("5c79715cb2e01c2ee3196a64"),
"name" : "asztal1"
}
]
},
{
"_id" : ObjectId("5c79718cb2e01c52afb050f9"),
"name" : "emelet",
"products" : [
{
"_id" : ObjectId("5c79719db2e01c52afb050fb"),
"active" : true,
"added" : null,
"description" : "sorocske baszod",
"modified" : null,
"name" : "sor",
"taxPercentage" : 27,
"unit" : "korso",
"unitPrice" : 500
},
{
"_id" : ObjectId("5c7971deb2e01c52afb050fd"),
"active" : true,
"added" : null,
"description" : "szaros viz",
"modified" : null,
"name" : "teodora",
"taxPercentage" : 27,
"unit" : "liter",
"unitPrice" : 1000
}
],
"qrcodes" : [
{
"_id" : ObjectId("5c797195b2e01c52afb050fa"),
"name" : "qr1"
}
]
},
{
"_id" : ObjectId("5c7a20ef2fa7fa847633bb39"),
"name" : "Addf",
"products" : [],
"qrcodes" : [
{
"_id" : ObjectId("5c7a20f92fa7fa847633bb3a"),
"name" : "Kisfaszom"
}
]
}
],
"postalCode" : "123",
"region" : "Közép-Magyarország",
"staffs" : [
{
"permissions" : [
"staffServing"
],
"_id" : ObjectId("5c7a6248b2e01c744f8b67b5"),
"email" : "varadi83gabor@gmail.com",
"modifyed" : null,
"name" : "12e12e",
"nickname" : "qweqwe",
"password" : "202cb962ac59075b964b07152d234b70",
"registered" : ISODate("2019-03-02T11:00:25.956Z"),
"status" : "active"
},
{
"permissions" : [
"staffFront",
"staffCourier",
"staffManager",
"staffServing"
],
"_id" : ObjectId("5c7a629fb2e01c744f8b67b7"),
"email" : "varadi83gabor@gmail.com",
"modifyed" : null,
"name" : "Kis Ferenc",
"nickname" : "123123",
"password" : "202cb962ac59075b964b07152d234b70",
"registered" : ISODate("2019-03-02T11:01:52.235Z"),
"status" : "active"
}
],
"status" : "active",
"street" : "qewqweqwe",
"streetNumber" : 123123
},
{
"_id" : ObjectId("5c7aa611b2e01c5565f70aff"),
"city" : "Budapest",
"country" : "Hungary",
"county" : "pest",
"currency" : "HUF",
"district" : "VI. kerület",
"language" : null,
"name" : "Kiado kocsma",
"phone" : "123123",
"places" : [],
"postalCode" : "1066",
"region" : "Közép-Magyarország",
"staffs" : [],
"status" : "active",
"street" : "Jokai",
"streetNumber" : 19
}
],
"status" : "inactive",
"street" : "890",
"streetNumber" : "890",
"taxNumber" : "890",
"__v" : 0
}
我必须通过_id在company.shops.staffs数组中找到一名工作人员,并且我还想将公司所有可用的商店都包括到输出文档中。
这是我尝试的汇总:
this.companyManager.aggregate([
{"$match": {"shops.staffs._id": body.staffId}},
{"$unwind": "$shops"},
{"$unwind": "$shops.staffs"},
{"$match": {"shops.staffs._id": body.staffId}},
{"$project": {_id: "$shops.staffs._id", shops: "$shops", staff: "$staffs"}},
], function(error, result:Null<Array<Dynamic>>) {
trace(error);
trace(result);
if (result.length == 0) {
callback({status: Status.ERROR, data: result, error: "not found staff"});
} else {
callback({status: (error == null) ? Status.SUCCESS : Status.ERROR, data: result[0].staff, error: error});
}
});
但这只是包括工作人员所在的商店。
如何生成像这样的oputput文档?:
{"staff":<staffData>,"shops":[shopsArray]}
/////// EDITED ////// 因此,我需要从shops.staff数组中找到一个工作人员,并从company.shops数组中找到所有商店,并将其记录在这样的文档中:
{
"staff": {
"permissions": [
"staffServing"
],
"_id": ObjectId("5c7a6248b2e01c744f8b67b5"),
"email": "varadi83gabor@gmail.com",
"modifyed": null,
"name": "12e12e",
"nickname": "qweqwe",
"password": "202cb962ac59075b964b07152d234b70",
"registered": ISODate("2019-03-02T11:00:25.956Z"),
"status": "active"
},
"shops": [<SHOPS DOCUMENTS>]
}
我希望现在已经清楚了,谢谢!
答案 0 :(得分:0)
尝试
我用过map
和filter
db.companyManager.aggregate([
{
'$addFields': {
'staff': {
'$filter': {
'input': {
'$map': {
'input': '$shops',
'as': 'shop',
'in': {
'staffs': {
'$filter': {
'input': '$$shop.staffs',
'as': 'staff',
'cond': {
'$eq': [
'$$staff._id', body.staffId
]
}
}
}
}
}
},
'as': 'staff',
'cond': {
'$gt': [
{
'$size': '$$staff.staffs'
}, 0
]
}
}
}
}
},
{$unwind: "$staff"},
{$project: {_id:0, staff: "$staff.staffs", shops: "$shops"} },
{$unwind: "$staff"}
])