java实现如下条件的mongodb查询

发布于 2022-09-06 12:23:29 字数 2530 浏览 12 评论 0

**用java操作,
条件:keyID=111111111111且point=A的所有record
这里的point是在remark里面的,而且只显示point=A和相应的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"
                }
            ]
        }
    ]
}

----------------------------查询结果应该如下---------------------------

{
    "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"
                }
            ]
        }
    ]
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你爱我像她 2022-09-13 12:23:29

$map,$reduce,$filter (segmentfault无法正确转义美元符号,凑合看吧……)这些运算符在很多场景下可以帮助我们避免$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"]
                }
            }
        }
    }
}])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文