【问题标题】:Delete Empty Documents in MongoDB删除 MongoDB 中的空文档
【发布时间】:2016-06-18 16:36:26
【问题描述】:

我想删除 MongoDB 中的空文档。 MongoDB 的查询应该是什么样子。我只想过滤掉并非每个字段都为空的文档。我只想删除那些所有字段都为 Null 的人

案例:

/* 1 */
{
    "_id" : ObjectId("57601e880855160fbb496f50"),
    "instemail" : [],
    "instProjekt1name" : [],
    "inststrasse" : [],
    "institutionID" : [],
    "insttelefax" : [],
    "insttelefon" : [],
    "institutionsname" : [],
    "instplz" : [],
    "insturl" : [],
    "instLand" : []
}

/* 2 */
{
    "_id" : ObjectId("57601e880855160fbb496f51"),
    "instemail" : [],
    "instProjekt1name" : [],
    "inststrasse" : [],
    "institutionID" : [],
    "insttelefax" : [],
    "insttelefon" : [],
    "institutionsname" : [],
    "instplz" : [],
    "insturl" : [],
    "instLand" : []
}

/* 3 */
{
    "_id" : ObjectId("57601e880855160fbb496f52"),
    "instemail" : [ 
        "postmaster@dfg.de"
    ],
    "instProjekt1name" : [ 
        "\n\t                                    ERA-Chemistry: Novel Pt-poor catalysts for the electrocatalytic O2 reduction based on modified, nanostructured metal oxides\n\t                                    ", 
        "\n\t                                    \"Welten\" von Mittelschichtangehörigen in zwei Satellitenstädten JABOTABEKs (Indonesien)\n\t                                    ", 
        "\n\t                                    Rätselspiele. Edition und Kommentar\n\t                                    ", 
        "\n\t                                    Analysis of the function of Presenilin in Development and Neurodegeneration in Drosophila melanogaster\n\t                                    ", 
        "\n\t                                    Estimating selection parameters and identifying loci under recent selection based on genome-wide sequence data\n\t                                    ", 
        "\n\t                                    Untersuchung polymerischer Stabilsation in kolloidalen Systemen unter Verwendung neuartiger feldtheoretischer Simulationen\n\t                                    ", 
        "\n\t                                    Entwicklung hämatopoietischer Stammzellen aus humanen ES- und iPS-Zellen\n\t                                    ", 
        "\n\t                                    Untersuchung polymerischer Stabilisation in kolloidalen Systemen unter Verwendung neuartiger feldtheoretischer Simulationen\n\t                                    "
    ],
    "inststrasse" : [ 
        "\r\n\t        \t            \t                Kennedyallee", 
        "40"
    ],
    "institutionID" : [ 
        "5000"
    ],
    "insttelefax" : [ 
        "+49 228 8852777"
    ],
    "insttelefon" : [ 
        "+49 228 8851"
    ],
    "institutionsname" : [ 
        "Deutsche Forschungsgemeinschaft"
    ],
    "instplz" : [ 
        "53175", 
        "Bonn"
    ],
    "insturl" : [ 
        "http://www.dfg.de"
    ],
    "instLand" : [ 
        "\r\n\t            \t            Deutschland\r\n\t        \t        "
    ]
}

预期:

/* 3 */
{
    "_id" : ObjectId("57601e880855160fbb496f52"),
    "instemail" : [ 
        "postmaster@dfg.de"
    ],
    "instProjekt1name" : [ 
        "\n\t                                    ERA-Chemistry: Novel Pt-poor catalysts for the electrocatalytic O2 reduction based on modified, nanostructured metal oxides\n\t                                    ", 
        "\n\t                                    \"Welten\" von Mittelschichtangehörigen in zwei Satellitenstädten JABOTABEKs (Indonesien)\n\t                                    ", 
        "\n\t                                    Rätselspiele. Edition und Kommentar\n\t                                    ", 
        "\n\t                                    Analysis of the function of Presenilin in Development and Neurodegeneration in Drosophila melanogaster\n\t                                    ", 
        "\n\t                                    Estimating selection parameters and identifying loci under recent selection based on genome-wide sequence data\n\t                                    ", 
        "\n\t                                    Untersuchung polymerischer Stabilsation in kolloidalen Systemen unter Verwendung neuartiger feldtheoretischer Simulationen\n\t                                    ", 
        "\n\t                                    Entwicklung hämatopoietischer Stammzellen aus humanen ES- und iPS-Zellen\n\t                                    ", 
        "\n\t                                    Untersuchung polymerischer Stabilisation in kolloidalen Systemen unter Verwendung neuartiger feldtheoretischer Simulationen\n\t                                    "
    ],
    "inststrasse" : [ 
        "\r\n\t        \t            \t                Kennedyallee", 
        "40"
    ],
    "institutionID" : [ 
        "5000"
    ],
    "insttelefax" : [ 
        "+49 228 8852777"
    ],
    "insttelefon" : [ 
        "+49 228 8851"
    ],
    "institutionsname" : [ 
        "Deutsche Forschungsgemeinschaft"
    ],
    "instplz" : [ 
        "53175", 
        "Bonn"
    ],
    "insturl" : [ 
        "http://www.dfg.de"
    ],
    "instLand" : [ 
        "\r\n\t            \t            Deutschland\r\n\t        \t        "
    ]
}

声明必须是什么样子?谁能帮帮我?

【问题讨论】:

  • IMO,您可以按大小找到这些文件!但是您需要知道您的文档类型的空文档的大小。 Document Size in MongoDb ;).

标签: mongodb documents


【解决方案1】:

我认为您应该在存在 = false 的字段上使用 $exists 进行调度和查询。

db.collection.find({ field1: { $exists: false},field2: { $exists: false}...})

https://docs.mongodb.com/manual/reference/operator/query/exists/

或者在$in的帮助下

{$in: [ [{}], [{}, {}] ] }

https://docs.mongodb.com/manual/reference/operator/query/in/

看看这个link

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 2016-09-23
    • 1970-01-01
    • 2013-09-14
    • 2016-04-20
    相关资源
    最近更新 更多