鍍金池/ 問答/Java  數(shù)據(jù)庫  HTML/ java實現(xiàn)如下條件的mongodb查詢

java實現(xiàn)如下條件的mongodb查詢

**用java操作,
條件:keyID=111111111111且point=A的所有record
這里的point是在remark里面的,而且只顯示point=A和相應(yīng)的city,其它的B,C都不要**

{
    "tempList": [
        {
            "_id": ObjectId("5a6afcfce9ec67213fca393d8"),
            "keyID": "111111111111",
            "price": 123,
            "remark": [
                {
                    "city": "beijing",
                    "point": "A"
                },
                {
                    "city": "shanghai",
                    "point": "A"
                },
                {
                    "city": "guangzhou",
                    "point": "C"
                }
            ]
        },
        {
            "_id": ObjectId("5a6afcfce2121fca393d8"),
            "keyID": "111111111111",
            "price": 456,
            "remark": [
                {
                    "city": "beijing",
                    "point": "A"
                },
                {
                    "city": "shanghai",
                    "point": "B"
                },
                {
                    "city": "guangzhou",
                    "point": "C"
                }
            ]
        },
         {
            "_id": ObjectId("5a6afcfce9e32577223d8"),
            "keyID": "222222222222",
            "price": 789,
            "remark": [
                {
                    "city": "beijing",
                    "point": "A"
                },
                {
                    "city": "shanghai",
                    "point": "B"
                },
                {
                    "city": "guangzhou",
                    "point": "C"
                }
            ]
        }
    ]
}

----------------------------查詢結(jié)果應(yīng)該如下---------------------------

{
    "tempList": [
        {
            "_id": ObjectId("5a6afcfce9ec67213fca393d8"),
            "keyID": "111111111111",
            "price": 123,
            "remark": [
                {
                    "city": "beijing",
                    "point": "A"
                },
                {
                    "city": "shanghai",
                    "point": "A"
                }
            ]
        },
        {
            "_id": ObjectId("5a6afcfce2121fca393d8"),
            "keyID": "111111111111",
            "price": 456,
            "remark": [
                {
                    "city": "beijing",
                    "point": "A"
                }
            ]
        }
    ]
}
回答
編輯回答
終相守

$map,$reduce,$filter (segmentfault無法正確轉(zhuǎn)義美元符號,湊合看吧……)這些運算符在很多場景下可以幫助我們避免$unwind,語法請參考下文檔。對于你的要求,可以用$filter直接解決問題:
文檔格式

{
    "_id" : ObjectId("5a6d7c5e0664b8343e7e126b"),
    "keyID" : "111111111111",
    "price" : 123,
    "remark" : [
        {
            "city" : "beijing",
            "point" : "A"
        },
        {
            "city" : "shanghai",
            "point" : "A"
        },
        {
            "city" : "guangzhou",
            "point" : "C"
        }
    ]
}

aggregation寫法

db.test.aggregate([{
    $match: {
        "remark.point": "A"
    }
}, {
    $project: {
        _id: 1,
        keyID: 1,
        price: 1,
        remark: {
            $filter: {
                input: "$remark",
                as: "remarks",
                cond: {
                    $eq: ["$$remarks.point", "A"]
                }
            }
        }
    }
}])
2018年4月27日 05:57