在MongoDB中,如何删除所有文档中具有特定值的字段?

问题描述

我有一个名为 employees

的集合

我想删除 employees 集合内所有文档中具有空值的所有字段。

这是我创建数据库的代码:

db.employees.insertMany( [ {"Name": "Stephen Jackson","Salary": 16099.55,"Position": "President","Rank": 1,"ReportingTo": null} ] )

db.employees.insertMany( [ {"Name": "Michael Harrison","Salary": 14567.12,"Position": "Vice- 
President","Rank": 2,"ReportingTo": "[President]"} ] )

db.employees.insertMany( [ {"Name": "Rex Andres","Salary": 13891.22,"Position": "Secretary","Rank": 3,"ReportingTo": "[Vice-President]"} ] )

db.employees.insertMany( [ {"Name": "Sam Johnson","Salary": 13000,"Position": "Treasurer","Rank": 
4,"ReportingTo": "[Secretary,Vice-President]"} ] )

这是我尝试做的:

db.employees.update( {"ReoportingTo": null},{"$unset": {"ReportingTo": ""}} )

我希望第一个文档中的字段被删除,因为它是唯一一个具有空值的字段。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)