Remove all fields that are null

前端 未结 5 497
-上瘾入骨i
-上瘾入骨i 2021-01-02 11:03

How can I remove all fields that are null from all documents of a given collection?


I have a collection of documents such as:

{
           


        
5条回答
  •  梦谈多话
    2021-01-02 11:39

    Like this question mentioned (mongodb query without field name):

    Unfortunately, MongoDB does not support any method of querying all fields with a particular value.

    So, you can either iterate the document (like Wizard's example) or do it in non-mongodb way.

    If this is a JSON file, remove all the lines with null in sed might works:

    sed '/null/d' ./mydata.json
    

提交回复
热议问题