0

I am using new Mongodb C# Driver 2.2.4, I have collection where i dont have all the fields in the document. For example

[BsonIgnoreExtraElements]
public class Category : Entity
{
    [BsonElement("name")]
    public string Name { get; set; }

    [BsonElement("title")]
    public string Title { get; set; }

    [BsonElement("description")]
    public string Description { get; set; }
}

Here is my query where i am only projecting name and title.

var category = All().Where(c => c.Name == "test")
                            .Select(c => new { c.Title, c.Name })
                            .FirstOrDefault();

My data looks like this

{ 
  "_id" : ObjectId("5575b9351eccba081c144433"), 
  "name" : "Sample Name", 
  "title" : "Sample Title", 
  "description" : "Sample Description"
}
{ 
  "_id" : ObjectId("5575b9351eccba081c144433"), 
  "name" : "Test", 
  "description" : "Test Description", 
}

Now since there is no Title with the document with Name = "Test" it throws error below.

No matching creator found. 
Anand Patel
  • 451
  • 5
  • 7

1 Answers1

0

check this link. you should try Query.exists

MongoDB how to check for existence

alternatively

MongoDB C# - Getting BsonDocument for an Element that doesn't exist

Community
  • 1
  • 1
KaSh
  • 175
  • 1
  • 11