3

I want to delete empty Documents in MongoDB. How should the query for MongoDB must look like. I only want to filter out the Documents where not every field is null. I only want to delete those one who has all fields equal Null

Case:

/* 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        "
    ]
}

Expected:

/* 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        "
    ]
}

How must the statement look like? Can anyone help me?

  • IMO, you can find those documents by their size! but you need to know the size of an empty document of your document type. [Document Size in MongoDb](http://stackoverflow.com/questions/12657690/document-size-in-mongodb) ;). – shA.t Jun 14 '16 at 19:11

1 Answers1

1

I think you should have scheduler and query with $exists on your fields that are exist=false.

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

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

Or with the help of $in

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

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

And take a look at this link

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Hamedz
  • 726
  • 15
  • 27