从MongoDB中的数组中删除一个元素?

时间:2018-05-29 15:56:36

标签: mongodb

我将以下文档存储在mongo DB集合中,我将动态接收要删除的url例如,我需要删除订阅者URL http://localhost.8080/FNOL/subscriber1以获取名称“name”:“FNOL”, “country”:“US”,“lob”:文件中的“财产”。

如何使用mongo编写remove命令?

我是否需要重新定义文档结构?

提前致谢。

{
        "_id" : ObjectId("5b07fbbc0d7a677d2f8b2d87"),
        "name" : "FNOL",
        "country" : "US",
        "lob" : "property",
        "subscribers" : [
                {
                        "protocol" : "REST",
                        "url" : "http://localhost.8080/FNOL/subscriber1"
                },
                {
                        "protocol" : "SOAP",
                        "url" : "http://localhost.8080/FNOL/subscriber2"
                },
                {
                        "protocol" : "JMS",
                        "url" : "NOTIFICATION.TOPIC.FNOL"
                }
        ]
}

删除后:

{
            "_id" : ObjectId("5b07fbbc0d7a677d2f8b2d87"),
            "name" : "FNOL",
            "country" : "US",
            "lob" : "property",
            "subscribers" : [

                    {
                            "protocol" : "SOAP",
                            "url" : "http://localhost.8080/FNOL/subscriber2"
                    },
                    {
                            "protocol" : "JMS",
                            "url" : "NOTIFICATION.TOPIC.FNOL"
                    }
            ]
    }

1 个答案:

答案 0 :(得分:1)

您可以使用$pull运营商指定上述条件,将匹配的文档和网址作为String str = "987654987855"; String max = String.valueOf(Integer.MAX_VALUE); String min = String.valueOf(Integer.MIN_VALUE); BigInteger b1 = new BigInteger(str); BigInteger b_max = new BigInteger(max); BigInteger b_min = new BigInteger(min); boolean isOutOfRange = b1.compareTo(b_max) > 0 || b1.compareTo(b_min) < 0; System.out.println(isOutOfRange); // Output true 的参数获取,如下所示:

$pull