如何使用 MongoDB 和 C# 驱动程序查询数组是否为空或为空?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11022265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to query if array is null or empty using MongoDB and C# Driver?
提问by Steve Konves
Background:
背景:
What I need to accomplish is to remove any records in a collection if a specific array on the record is null or empty.
如果记录上的特定数组为空或为空,我需要完成的是删除集合中的任何记录。
I understand that the C# Driver query to find a null array is:
我知道查找空数组的 C# 驱动程序查询是:
IMongoQuery query = Query.Exists("myArray", false);
That is fine for detecting an null array, but sometimes the array will not be null, but will not have any elements. What I need is more like:
这对于检测空数组很好,但有时数组不会为空,但不会有任何元素。我需要的更像是:
// Note: second subquery will not work
IMongoQuery query = Query.Or(
Query.Exists("myArray", false),
Query.IsEmpty("myArray", false) // error
);
Model:
模型:
My class would look like:
我的班级看起来像:
public class MyClass
{
// This property may be null or empty
[BsonElement("myArray")]
public string[] MyArray { get; set; }
[BsonElement("someElement")]
public int SomeElement{ get; set; }
}
Summary:
概括:
- What C# Driver method should I use to query if an array is empty?
- Or, what is the best way to check if an array is null or empty?
- 我应该使用什么 C# 驱动程序方法来查询数组是否为空?
- 或者,检查数组是 null 还是空的最佳方法是什么?
Any help with this would be greatly appreciated! :)
对此的任何帮助将不胜感激!:)
采纳答案by Craig Wilson
You are looking for the $size operator.
您正在寻找 $size 运算符。
Query.Size("myArray", 0)will be true if the array is empty.
Query.Size("myArray", 0)如果数组为空,则为真。
http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24size
http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24size
回答by Poorna
$exists matches only fields that are not present. If a field is present, but it's value is null (BSON null), then those fields aren't matched by $exists. http://docs.mongodb.org/manual/reference/operator/query/exists/
$exists 只匹配不存在的字段。如果某个字段存在,但其值为空(BSON 空),则这些字段与 $exists 不匹配。http://docs.mongodb.org/manual/reference/operator/query/exists/
However, checking the fields value against null matches in both cases: the field doesn't exist, and also the field value is null. http://docs.mongodb.org/manual/faq/developers/#faq-developers-query-for-nulls
但是,在两种情况下检查字段值与空匹配:该字段不存在,并且该字段值为空。http://docs.mongodb.org/manual/faq/developers/#faq-developers-query-for-nulls
So, in this case. It should be OR condition on two criterias: 1. field: null 2. field size =0;
所以,在这种情况下。它应该是基于两个条件的 OR 条件:1. 字段:空 2. 字段大小 =0;
{ "$or" : [ { "MyArray" : { "$size" : 0}} , { "MyArray" : null }]}
{ "$or" : [ { "MyArray" : { "$size" : 0}} , { "MyArray" : null }]}
回答by viren Kalkhudiya
To find blank array fields in MongoDb collection use the following query in Mongo shell
db.COLLECTION.find({ myArray: [] }).pretty();
要在 MongoDb 集合中查找空白数组字段,请在 Mongo shell 中使用以下查询
db.COLLECTION.find({ myArray: [] }).pretty();
Change COLLECTION with your DB collection name.
使用您的数据库集合名称更改 COLLECTION。
回答by Mikael Lirbank
Note that $sizecan't use indexesand $orhas a few index related limitationstoo. I've found the following to be a good way to query for documents with empty or non-set arrays, while avoiding the limitations of $or and $size. And it is short too! :)
请注意,$size不能使用索引并且$or也有一些与索引相关的限制。我发现以下是查询具有空数组或未设置数组的文档的好方法,同时避免了 $or 和 $size 的限制。而且还很短!:)
{myArray: {$in: [null, []]}}
{myArray: {$in: [null, []]}}

