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
