6

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:

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:

  1. What C# Driver method should I use to query if an array is empty?
  2. Or, what is the best way to check if an array is null or empty?

Any help with this would be greatly appreciated! :)

4 Answers 4

14

You are looking for the $size operator.

Query.Size("myArray", 0) will be true if the array is empty.

http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24size

Sign up to request clarification or add additional context in comments.

Comments

5

$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/

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

So, in this case. It should be OR condition on two criterias: 1. field: null 2. field size =0;

{ "$or" : [ { "MyArray" : { "$size" : 0}} , { "MyArray" : null }]}

2 Comments

This appears to be wrong. MongoDB doesn't allow the $or or $and operators for arrays. I tried this in the mongo shell, and got error: { "$err" : "invalid operator: $or", "code" : 10068 }.
I just did the following from console and It worked without any errors. >db.array.insert({MyArray:[12, 23]}) > db.array.find() { "_id" : ObjectId("53ec3aac1f4db058b95d435e"), "MyArray" : [ 12, 23 ] } > db.array.find({$or:[{MyArray:{$size:0}}, {MyArray:null}]}) >
5

Note that $size can't use indexes and $or has a few index related limitations too. 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! :)

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

Comments

3

To find blank array fields in MongoDb collection use the following query in Mongo shell db.COLLECTION.find({ myArray: [] }).pretty();

Change COLLECTION with your DB collection name.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.