查找具有空值的字段并将它们计数在 MongoDB 中
Find fields with null values and count them in MongoDB
我想在我的数据库中查找包含空值的字段。我在 Whosebug 上的其他一些线程中读到,您可以尝试使用 $ne(不等于),但它对我不起作用,所以我认为这可能是我做错的其他事情。
这是我的数据库中的一个示例:
{ "_id" : ObjectId("5695553244aec8a25d162698"), "DBLPKey" : "journals/software/Goth08a", "LastModified" : "2011-11-07", "Authors" : [ { "Name" : "Greg Goth", "Affiliation" : null } ], "Title" : "Ultralarge Systems: Redefining Software Engineering?", "Pages" : "91-94", "Year" : "2008", "Volume" : "25", "Journal" : "IEEE Software", "Number" : "3", "ElectronicEdition" : "http://doi.ieeecomputersociety.org/10.1109/MS.2008.82", "LocalURL" : "db/journals/software/software25.html#Goth08a", "Abstracts" : "Ultra-Large-Scale Systems: The Software Challenge of the Future, a report produced by Carnegie Mellon University's Software Engineering Institute, just might be a watershed blueprint for the next generation of top-level software design. Although it's written with a distinct slant toward the US military's future requirements, its description of how the fundamental principles of software design will change in a global economy—defined by ubiquitous computing—are finding wide appeal." }
我想在这里找到的字段是Author 中名为Affiliation 的字段。
我试过这个查询:
db.Articles.find({Affiliation:{ $ne: null}})
但这对我不起作用。是因为我必须以某种方式告诉 MongodDB 它在 Author 字段中吗?还是我误解或漏掉了其他一些东西?
您可以使用elemMatch
试试这个,
db.Articles.find({ "Authors": { $elemMatch: {"Affiliation" : null } } } )
我想在我的数据库中查找包含空值的字段。我在 Whosebug 上的其他一些线程中读到,您可以尝试使用 $ne(不等于),但它对我不起作用,所以我认为这可能是我做错的其他事情。
这是我的数据库中的一个示例:
{ "_id" : ObjectId("5695553244aec8a25d162698"), "DBLPKey" : "journals/software/Goth08a", "LastModified" : "2011-11-07", "Authors" : [ { "Name" : "Greg Goth", "Affiliation" : null } ], "Title" : "Ultralarge Systems: Redefining Software Engineering?", "Pages" : "91-94", "Year" : "2008", "Volume" : "25", "Journal" : "IEEE Software", "Number" : "3", "ElectronicEdition" : "http://doi.ieeecomputersociety.org/10.1109/MS.2008.82", "LocalURL" : "db/journals/software/software25.html#Goth08a", "Abstracts" : "Ultra-Large-Scale Systems: The Software Challenge of the Future, a report produced by Carnegie Mellon University's Software Engineering Institute, just might be a watershed blueprint for the next generation of top-level software design. Although it's written with a distinct slant toward the US military's future requirements, its description of how the fundamental principles of software design will change in a global economy—defined by ubiquitous computing—are finding wide appeal." }
我想在这里找到的字段是Author 中名为Affiliation 的字段。 我试过这个查询:
db.Articles.find({Affiliation:{ $ne: null}})
但这对我不起作用。是因为我必须以某种方式告诉 MongodDB 它在 Author 字段中吗?还是我误解或漏掉了其他一些东西?
您可以使用elemMatch
试试这个,
db.Articles.find({ "Authors": { $elemMatch: {"Affiliation" : null } } } )