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");

 posted on 2017-07-12 10:44  jayruan  阅读(172)  评论(0编辑  收藏  举报