mongo views

db.itemsView.drop();

db.items.aggregate([
{
"$match": {
"status": "true"
}
},
{
"$unwind": {
"path": "$globalOptions",
"preserveNullAndEmptyArrays": true
}
},
{
"$lookup": {
"from": "globaloptiongroups",
"localField": "globalOptions",
"foreignField": "_id",
"as": "globaloptiongroups"
}
},
{
"$unwind": {
"path": "$globaloptiongroups",
"preserveNullAndEmptyArrays": true
}
},
{
"$group": {
"_id": "$_id",
"globaloptiongroups": {
"$push": "$globaloptiongroups"
},
"name": {
"$first": "$name"
},
"merchantId": {
"$first": "$merchantId"
},
"customerOptions": {
"$first": "$customerOptions"
},
"properties": {
"$first": "$properties"
},
"price": {
"$first": "$price"
},
"picture": {
"$first": "$picture"
},
"description": {
"$first": "$description"
},
"category": {
"$first": "$category"
},
"order": {
"$first": "$order"
}
}
},
{
"$project": {
"_id": 1,
"name": 1,
"merchantId": 1,
"options": 1,
"price": 1,
"picture": 1,
"description": 1,
"category": 1,
"order": 1,
"groupOptions": {
"$setUnion": [
"$groupOptions",
"$customerOptions"
]
}
}
},
{
"$sort": {
"order": 1
}
}
]).saveAsView("itemsView");

原文地址:https://www.cnblogs.com/jayruan/p/7154212.html