Jamie
Jamie

Reputation: 321

Query mongodb with C# driver not working

I have the following error when pasting examples from the internet to query our mongodb and look for a field.

I can't see what I'm doing wrong, think I'll go back to SQL if I can't work it out soon :(

I just want to be able to say find me the cars with 3 doors.

Thanks for any help! Much appreciated.

Connection code

public MongoDatabase GetDatabase
{
    get
    {
        MongoClient mc = new MongoClient();
        var server = mc.GetServer();
        return server.GetDatabase("players");
    }
 }

Code sample

var numberOfDoors = 3;

var collection = GetDatabase.GetCollection<CarCollection>("Cars");

// Have tried using my Car object instead of BsonDocument but little difference
var filter = Builders<BsonDocument>.Filter.Eq("NumberOfDoors", numberOfDoors);

var result = collection.Find(filter);

Error message

Argument 1: cannot convert from 'MongoDB.Driver.FilterDefinition<MongoDB.Bson.BsonDocument>' to 'MongoDB.Driver.IMongoQuery'

Document structure

{
    "_id" : ObjectId("..."),
    "Members" : [ 
        {
            "EmailAddress" : "",
            ...
        }, 
        {
            "EmailAddress" : "",
            ...
        }, 
        {
            "EmailAddress" : "",
            ...
        }
    ]
}

C# structure

public class MemberCollection
{
    public ObjectId _id { get; set; }

    public MemberEntity[] Members { get; set; }
}

public class MemberEntity
{
    public int _id { get; set; }

    public string Password { get; set; }

    public string EmailAddress { get; set; }
}

Update - showing error message

enter image description here

Document structure

enter image description here

Update #2

enter image description here

Update 3

enter image description here

Upvotes: 3

Views: 7207

Answers (2)

Rahul
Rahul

Reputation: 77876

Your below line is wrong

Builders<BsonDocument>.Filter.Eq("NumberOfDoors", numberOfDoors);

It rather should be below since you are trying to place a filter on CarCollection. That's the source of error.

Builders<CarCollection>.Filter.Eq("NumberOfDoors", numberOfDoors);

Per your recent edit, you should change the filter definition to below since you are actually trying to place a filter on a nested array of member objects

var filter = Builders<MemberCollection>.Filter.Eq("Members.EmailAddress", emailAddress);

The equivalent MongoDB query would be

db.Getcollection('Members').Find({"Members.EmailAddress": "[email protected]"})

EDIT:

Not sure what's wrong @ your end but give it a try like below

var collection = _mongoclientProvider.GetDatabase.GetCollection<MemberCollection>("Members");
Expression<Func<MemberCollection, bool>> expression = mc => mc.Members.Any(m => m.EmailAddress == emailAddress);
var list = collection.Find(expression).ToList();

Upvotes: 5

Nick Bull
Nick Bull

Reputation: 9866

You're using Builders<CarCollection>.Filter.Eq() incorrectly:

var numberOfDoors = 3;

var collection = ..GetCollection<CarCollection>("Cars");

var builder = Builders<CarCollection>.Filter;
var filt = builder.Where(car => car.NumberOfDoors == numberOfDoors);
var list = await collection.Find(filt).ToListAsync();

This assumes that:

public class CarCollection : IEnumerable<Car> { ... }

and

public class Car { public int NumberOfDoors { get; set; } ... }

Upvotes: 1

Related Questions