У меня есть документы, подобные этому:
{
"_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
}
Мне нужно найти одного из сотрудников из массива company.shops.staffs по _id, и я также хочу включить все доступные магазины от компании довыходной документ.
Вот агрегация, которую я пробую:
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});
}
});
, но это всего лишь магазин сотрудников, где работают.
Как я могу генерировать oputputdoc, как этот?:
{"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>]
}
Надеюсь, теперь все ясно, спасибо!