MONGOOSE - 仅显示我在对象内输入的特定值

时间:2018-03-23 07:55:34

标签: javascript node.js mongodb mongoose

美好的一天,我花了2-3个小时才开始工作,

所以我有这个controller.js代码:

const b_name = req.params.b;
const m_name = req.params.m;
const result = await cinemaDB.getcinemasbranchesmovies(model, {'details.branches_name': b_name, 'details.movies_name': m_name});

和这个db.js代码:

/** Get  branches's movies by _id */
module.exports.getcinemasbranchesmovies =
async(model, query) => {
return new Promise((resolve, reject) => {

    // console.log("branches: " + b_name + "movies: "+ m_name);
    if (!query) {
      query = {};
    }

    console.log("branches: ", query);
    model.find(query)
        .then(res => {
            console.log("branches: ", res);
            resolve(res);
        })
        .catch(err => {
            reject(err);
            console.log(err);
            throw err;
        });

});
}

和这个route.js代码:

server.get("/api/cinemas/getcbm/:b/:m",controller.getcinemasbranchesmovies);

EDIT!如果你把它放在查询中:

model.find({'details.branches_name': "Evia", 'details.movies_name': "Greatest"})

现在这是JSON结果:

{
"success": true,
"status": "OK",
"data": [
    {
        "_id": "5ab49348c356c713a836cc15",
        "cinemas_name": "Cinemas 3",
        "__v": 0,
        "details": [
            {
                "price": 350,
                "movies_name": "Black Panther",
                "branches_name": "Bataan",
                "_id": "5ab49348c356c713a836cc16",
                "time": [
                    "1",
                    "2",
                    "3"
                ]
            },
            {
                "branches_name": "Bataan",
                "movies_name": "Black Panther",
                "price": 350,
                "_id": "5ab494b2c0a83c28e874ace9",
                "time": [
                    "1",
                    "2",
                    "3"
                ]
            },
            {
                "branches_name": "Bataan",
                "movies_name": "Black Panther",
                "price": 350,
                "_id": "5ab495a9d0bebb3d4c26c086",
                "time": [
                    "1",
                    "2",
                    "3"
                ]
            },
            {
                "branches_name": "Bataan",
                "movies_name": "December Avenue",
                "price": 350,
                "_id": "5ab495c5d0bebb3d4c26c087",
                "time": [
                    "1",
                    "2",
                    "3"
                ]
            },
            {
                "branches_name": "Taguig",
                "movies_name": "December Avenue",
                "price": 350,
                "_id": "5ab49d4b2b0ce727dc9a2a07",
                "time": [
                    "1",
                    "2",
                    "3"
                ]
            },
            {
                "branches_name": "Evia",
                "movies_name": "December Avenue",
                "price": 350,
                "_id": "5ab49d522b0ce727dc9a2a08",
                "time": [
                    "1",
                    "2",
                    "3"
                ]
            },
            {
                "branches_name": "Evia",
                "movies_name": "Greatest",
                "price": 350,
                "_id": "5ab4a0e397ca122e8005c19f",
                "time": [
                    "1",
                    "2",
                    "3"
                ]
            }
        ]
    }
]
}

现在我只需要显示这个值:

{
"success": true,
"status": "OK",
"data": [
            {
                "_id": "5ab49348c356c713a836cc15",
                "cinemas_name": "Cinemas 3",
                "__v": 0,
                "details": [
                    {
                        "branches_name": "Evia",
                        "movies_name": "Greatest",
                        "price": 350,
                        "_id": "5ab4a0e397ca122e8005c19f",
                        "time": [
                            "1",
                            "2",
                            "3"
                        ]
                    }
                ]
            }
        ]
    }

总而言之,我只想显示我在查询查询中输入的特定数据。 我先在google上搜索它,但我无法理解它们编码的方式。

1 个答案:

答案 0 :(得分:0)

所以,是的,扩展我对谷歌的理解,我通过使用聚合函数完成它。

替换model.find - > model.aggregate

它的查询类似于:

model.aggregate({$unwind:"$details"}, 
                {
                    $match:{
                        "details.branches_name": query1, 
                        "details.movies_name": query2
                    } 
                } 
            )

我希望有人能帮忙:D