Reputation: 4453
I have long binary blob (a stored PNG actually with miniature) in MongoDB. I need a filter that will return all slugs with non-empty blob.
I tried something like this:
var f = Builders<Common.Json.Database.Picture>.Filter.Where(pic => pic.ContentMini.Length > 0 && pic.ContentFull.Length > 0);
var p = Builders<Common.Json.Database.Picture>.Projection.Include(x => x.Slug);
var s = Builders<Common.Json.Database.Picture>.Sort.Ascending(x => x.Slug);
return Collections.Pictures.Find(f)
.Project<Common.Json.Database.Picture>(p)
.Sort(s)
.ToList()
.Select(x => x.Slug);
But this returns empty set. If I change Filter
to Filter.Empty
I am recieving all slugs in my collection.
The collection definition is below:
[System.Serializable]
public class Picture
{
[BsonId]
[JsonIgnore]
[NonSerialized]
public ObjectId InternalId;
[BsonElement("slug")]
[JsonProperty("slug")]
public string Slug;
[BsonElement("author")]
[JsonProperty("author")]
public Common.Author Author;
[BsonElement("owner")]
[BsonRepresentation(BsonType.String)]
[JsonProperty("owner")]
public Guid OwningUserOrGroup;
[BsonElement("keywords")]
[JsonProperty("keywords")]
public List<string> Keywords = new List<string>();
[BsonElement("last_updated")]
[JsonProperty("last_updated")]
public DateTime LastUpdated;
[BsonElement("is_approved")]
[JsonProperty("is_approved")]
public bool IsApproved;
[BsonElement("is_obsolete")]
[JsonProperty("is_obsolete")]
public bool IsObsolete;
[BsonElement("content_mini")]
[BsonRepresentation(BsonType.Binary)]
[JsonIgnore]
public byte[] ContentMini;
[BsonElement("content_full")]
[BsonRepresentation(BsonType.Binary)]
[JsonIgnore]
public byte[] ContentFull;
}
Upvotes: 1
Views: 1546
Reputation: 49975
Empty byte
array gets stored in MongoDB as "content_mini" : BinData(0,"")
so you can simply use $ne
to build your query in Mongo shell
db.pictures.find( {"content_mini": { $ne: new BinData(0,"") } } )
To build that query in C# you need Builders
generic type and Ne
method. To filter out all empty arrays and null
values you can use below code:
var filter = Builders<Picture>.Filter.Ne(f => f.ContentMini, new byte[] { });
var filter2 = Builders<Picture>.Filter.Ne(f => f.ContentMini, null);
var result = Col.Find(filter & filter2).ToList();
You shouldn't use Filter.Empty
because it actaully represents empty query, just like db.col.find({})
in Mongo shell
Upvotes: 2