2

These are my entities;

class User
{
    [BsonId]
    public string Id { get; set; }
    public string name { get; set; }
    public List<Address> Adress { get; set; }
}

class Address
{
    [BsonId]
    public string Id { get; set; }
    public string AddressName { get; set; }
    public string AddressDetail { get; set; }

}

My insert code to mongo db as below , there is no problem.

        const string uri = "mongodb://usermehmet:inno12345@localhost/testDB";
        var client = new MongoClient(uri);
        var db = client.GetServer().GetDatabase(new MongoUrl(uri).DatabaseName);

        counter++;

        User user=new User();
        user.Id = ObjectId.GenerateNewId().ToString();
        user.name = counter+"user";
        user.Adress = new List<Address>();

        Address a1=new Address();
        a1.Id = ObjectId.GenerateNewId().ToString();
        a1.AddressName = "Ev";
        a1.AddressDetail = "a sokak b caddesi c no d kat";

        Address a2=new Address();
        a2.Id = ObjectId.GenerateNewId().ToString();
        a2.AddressName = "İş";
        a2.AddressDetail = "x sokak y caddesi z no f kat";

        user.Adress.Add(a1);
        user.Adress.Add(a2);

        var collection2 = db.GetCollection<User>("Users");
        collection2.Insert(user);

But my query operations that I take from mongodb website (Query an Array for an Element¶) are creating editor error at var result = collection.Find(filter).ToList(); line.

 const string uri = "mongodb://usermehmet:inno12345@localhost/testDB";

        var client = new MongoClient(uri);
        var db = client.GetServer().GetDatabase(new MongoUrl(uri).DatabaseName);


        var collection = db.GetCollection<User>("Users");

        var filter = Builders<User>.Filter.Eq("name", "100user");
        var result = collection.Find(filter).ToList();

Error:

cannot convert from MongoDB.Driver.FilterDefinition ConsoleApplication14.User to MongoDB.Driver.IMongoQuery

Error image:

1
  • It's strange, your code seems ok, maybe the problem is in usings... try to use lambda instead collection.Find(x => x.name == "100user").ToList(); Commented Jan 23, 2018 at 9:16

2 Answers 2

3

You're probably using an older version of the MongoDB .NET Driver, as evident from the GetServer() call. In the old API, the Find() function expected an IMongoQuery filter definition.

Try to update your MongoDB.Driver NuGet. The latest is v2.5 if I'm not mistaken.

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

3 Comments

my all driver is v2.5.0 , so is problem may be from getserver() , there is warning that : " GetServer() is obsolete use the new api instead"
could you please ,may you share find operations by new api
Not sure I understand your question, but try to remove the obsolete call.
0

I resolved problem by mongo db new api methods ,problem was GetServer because it is obsolete.

MongoClientSettings settings = MongoClientSettings.FromUrl(new MongoUrl("mongodb://usermehmet:inno12345@localhost/testDB"));
        var mongoClient = new MongoClient(settings);
        _database = mongoClient.GetDatabase("testDB");

        var collection=_database.GetCollection<User>("Users");
        var result = collection.Find(x=>x.name=="100user").ToList();

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.