Mongo DB:从集合中删除文档

时间:2015-11-20 09:53:53

标签: mongodb

这是我收藏的“实体”文件之一,我无法弄清楚如何将其删除。

 {
    "_id" : {
        "id" : "sensors:StreetLight2",
        "type" : "sensors",
        "servicePath" : "/egmmqttpath"
    },
    "attrNames" : [
        "TimeInstant",
        "PING_status"
    ],
    "attrs" : {
        "PING_status" : {
            "value" : "delivered but no respond",
            "type" : "string",
            "md" : [
                {
                    "name" : "TimeInstant",
                    "type" : "ISO8601",
                    "value" : "2015-11-20T09:02:53.114688"
                }
            ],
            "creDate" : 1448010161,
            "modDate" : 1448010172
        },
        "TimeInstant" : {
            "value" : "2015-11-20T09:02:53.114834",
            "type" : "ISO8601",
            "creDate" : 1448010122,
            "modDate" : 1448010172
        }
    },
    "creDate" : 1448010122,
    "modDate" : 1448010172
}

有什么想法吗?我该如何删除上述文件?提前谢谢。

5 个答案:

答案 0 :(得分:1)

我会通过_id删除它。

db.entities.remove({
    "_id" : {
        "id" : "sensors:StreetLight2",
        "type" : "sensors",
        "servicePath" : "/egmmqttpath"
    }
})

答案 1 :(得分:1)

来自doc

  

db.collection.remove()

     

从集合中删除文档。

1)按_ID:由于_ID是唯一的。

db.entities.remove( {"_id" :
    {
        "id" : "sensors:StreetLight2",
        "type" : "sensors",
        "servicePath" : "/egmmqttpath"
    }
})

答案 2 :(得分:-2)

mongo shell中,您应该使用.remove()

db.entities.remove(
{
    "_id" :
    {
        "id" : "sensors:StreetLight2",
        "type" : "sensors",
        "servicePath" : "/egmmqttpath"
    }
})

答案 3 :(得分:-2)

删除mongodb中的文档

query = {"_id": <value of _id>}

db.entities.remove(query)

答案 4 :(得分:-2)

db.entities.remove(
 "_id" : {
"id" : "sensors:StreetLight2",
        "type" : "sensors",
        "servicePath" : "/egmmqttpath""id" : "sensors:StreetLight2"
    }
)

您还可以从文档中删除

中的任何键值对

您可以参考这些链接以便更好地理解

https://docs.mongodb.org/manual/tutorial/remove-documents/

https://docs.mongodb.org/manual/reference/method/db.collection.remove/