Доброго времени суток, благородные пользователи mongoDB. У меня проблема с перемещением одного встроенного документа на уровне документа с определенным условием.
Этот запрос:
db.products_oil.aggregate([
{
'$lookup': {
'from': 'brands',
'localField': 'brand_id',
'foreignField': '_id',
'as': 'brand'
}
},
{
'$lookup': {
'from': 'categories',
'localField': 'category_id',
'foreignField': '_id',
'as': 'category'
}
},
{'$unwind': "$brand"},
{'$unwind': "$category"},
{
'$sort': {
'position': 1
}
}
]).pretty()
дает мне такую структуру:
{
"_id" : ObjectId("5f08a990d4a40117fbe1e679"),
"createdAt" : ISODate("2020-07-10T20:46:56.282Z"),
"brand_id" : ObjectId("5ef51c165ff59a574439fd0b"),
"category_id" : ObjectId("5efa20238a909a3d4c52110a"),
"subCategory_id" : ObjectId("5efa2bc82480962e22ae096d"),
"position" : 2,
"active" : 1,
"productName" : "qwe",
"productDescription" : "<p>asd</p>",
"productSpec" : "<p>qwe</p>",
"productImgPath" : "files/1594403209__me.jpg",
"productPdf1Path" : "files/pdf/1594403212__20191120-948-cunncj-pdf.pdf",
"productPdf2Path" : "files/pdf/1594403214__20191120-948-cunncj-pdf.pdf",
"brand" : {
"_id" : ObjectId("5ef51c165ff59a574439fd0b"),
"createdAt" : ISODate("2020-06-26T00:50:14.207Z"),
"position" : 3,
"active" : 1,
"brandImgPath" : "files/1593121813__5.png",
"brandName" : "xcvxz"
},
"category" : {
"_id" : ObjectId("5efa20238a909a3d4c52110a"),
"createdAt" : ISODate("2020-06-29T20:08:51.035Z"),
"position" : 2,
"active" : 1,
"categoryType" : 1,
"categoryName" : "dccccccccc",
"categoryDescription" : "<p>cccccccccccccccccc</p>",
"subCategories" : [
{
"sub_id" : ObjectId("5efa2bc12480962e22ae0969"),
"createdAt" : ISODate("2020-06-29T20:58:25.894Z"),
"position" : 3,
"active" : 1,
"subCategoryName" : "sscdc",
"subCategoryDescription" : "<p>csdcsdcsdc</p>"
},
{
"sub_id" : ObjectId("5efa2bc82480962e22ae096d"),
"createdAt" : ISODate("2020-06-29T20:58:32.481Z"),
"position" : 2,
"active" : 1,
"subCategoryName" : "safasdfasdf",
"subCategoryDescription" : "<p>asfvt3333</p>"
},
{
"sub_id" : ObjectId("5efcb3e624f3a0d6351f9b69"),
"createdAt" : ISODate("2020-07-01T19:03:50.189Z"),
"position" : 1,
"active" : 0,
"subCategoryName" : "asd",
"subCategoryDescription" : "<p>asd</p>"
}
]
}
}
Мне нужно поместить объект category.subCategories как «подкатегорию» на самый верхний уровень (уровень самой коллекции), где category.subCategories.sub_id === subCategory_id
, чтобы иметь что-то вроде:
{
"_id" : ObjectId("5f08a990d4a40117fbe1e679"),
"createdAt" : ISODate("2020-07-10T20:46:56.282Z"),
"brand_id" : ObjectId("5ef51c165ff59a574439fd0b"),
"category_id" : ObjectId("5efa20238a909a3d4c52110a"),
"subCategory_id" : ObjectId("5efa2bc82480962e22ae096d"),
"position" : 2,
"active" : 1,
"productName" : "qwe",
"productDescription" : "<p>asd</p>",
"productSpec" : "<p>qwe</p>",
"productImgPath" : "files/1594403209__me.jpg",
"productPdf1Path" : "files/pdf/1594403212__20191120-948-cunncj-pdf.pdf",
"productPdf2Path" : "files/pdf/1594403214__20191120-948-cunncj-pdf.pdf",
"brand" : {
"_id" : ObjectId("5ef51c165ff59a574439fd0b"),
"createdAt" : ISODate("2020-06-26T00:50:14.207Z"),
"position" : 3,
"active" : 1,
"brandImgPath" : "files/1593121813__5.png",
"brandName" : "xcvxz"
},
"category" : {
"_id" : ObjectId("5efa20238a909a3d4c52110a"),
"createdAt" : ISODate("2020-06-29T20:08:51.035Z"),
"position" : 2,
"active" : 1,
"categoryType" : 1,
"categoryName" : "dccccccccc",
"categoryDescription" : "<p>cccccccccccccccccc</p>",
"subCategories" : [
{
"sub_id" : ObjectId("5efa2bc12480962e22ae0969"),
"createdAt" : ISODate("2020-06-29T20:58:25.894Z"),
"position" : 3,
"active" : 1,
"subCategoryName" : "sscdc",
"subCategoryDescription" : "<p>csdcsdcsdc</p>"
},
{
"sub_id" : ObjectId("5efa2bc82480962e22ae096d"),
"createdAt" : ISODate("2020-06-29T20:58:32.481Z"),
"position" : 2,
"active" : 1,
"subCategoryName" : "safasdfasdf",
"subCategoryDescription" : "<p>asfvt3333</p>"
},
{
"sub_id" : ObjectId("5efcb3e624f3a0d6351f9b69"),
"createdAt" : ISODate("2020-07-01T19:03:50.189Z"),
"position" : 1,
"active" : 0,
"subCategoryName" : "asd",
"subCategoryDescription" : "<p>asd</p>"
}
]
},
"subcategory" : {
"sub_id" : ObjectId("5efa2bc82480962e22ae096d"),
"createdAt" : ISODate("2020-06-29T20:58:32.481Z"),
"position" : 2,
"active" : 1,
"subCategoryName" : "safasdfasdf",
"subCategoryDescription" : "<p>asfvt3333</p>"
},
}
Совершенно запутался в раскрутках, группах и проектах: (