MongoDB подтолкнуть значение к другому объекту или объединить объект и значение ключа - PullRequest
0 голосов
/ 06 марта 2019

Предположим, у вас есть следующие документы в моей коллекции:

{
    "_id": ObjectId("5c7e3982a50f3b32d2668112"),
    "cat_id": 1,
    "cat_code": "YMN",
    "overall": {
        "temp_price": NumberInt(500),
    },
    "date_wise": [{
            "last_inserted_id": "11",
            "created_at": "2018-07-26",
            "overall": {
                "temp_fee": NumberInt(20),
                // .....
            },
        }, {
            "last_inserted_id": NumberInt(50),
            "created_at": "2018-08-01",
            "overall": {
                "temp_fee": NumberInt(100),
                // .....
            },
            "data": []
        }, {
            "last_inserted_id": "28",
            "created_at": "2018-08-02",
            "overall": {
                "temp_fee": NumberInt(22),
                // .....    
            },
        }
        // .....
    ]
} 

Я хочу вывод как это:

{
    "_id": {
        "created_at": "2018-07-26",
        "temp_fee": NumberInt(20),
    },
    "_id": {
        "created_at": "2018-08-01",
        "temp_fee": NumberInt(100),
    },
    "_id": {
        "created_at": "2018-08-02",
        "temp_fee": NumberInt(22),
    }
}

Я не профессионал в MongoDB, все еще на стадии обучения. Итак, мои усилия заключаются в следующем.

db.collection.aggregate([
    {$match : {'cat_code': 'YMN'}},
    { $project: {overall1: "$date_wise.overall"}},
    { $group: { _id: '$overall1'}},
    {'$unwind':'$_id'},
    {'$skip':0},
    {'$limit':10},
    {'$sort': {'_id.created_at' : 1}}
]);

Я пытаюсь объединить одно значение overall, т.е. created_at и данные overall внутри date_wise ключа. Подать заявку на сортировку, пропустить и ограничить ее.

...