YMC
YMC

Reputation: 5452

.NET driver for MongoDb: Is any way to avoid property names hardcoding?

I can use collection.Insert<T> for inserting instances and collection.Save<T> to update them, and I see no reasons why MongoDb's c# driver does not provide similar method to remove specific instance. Their Remove method asks for IMongoQuery argument which seems to require hardcoding property names like Query.EQ("Id", BsonValue.Create(id)). I'd like to specify whole instance to delete it like Remove<T>, or having RemoveById, or build LINQ-like query with specific input type, or any other way to get rid of property hard-coding, - the things I get used in MS Entity Framework POCO. Is any such a way?

UPDATE: Ok, I've found one way to do it:

IMongoQuery mq = new QueryDocument(entity.ToBsonDocument());
customers.Remove(mq);

is it the only way?

Thanks

Upvotes: 0

Views: 678

Answers (1)

Robert Stam
Robert Stam

Reputation: 12187

There is not yet a way to do what you are asking for but it is a frequently asked for feature. You can vote for the feature on this JIRA request:

https://jira.mongodb.org/browse/CSHARP-457

The approach you found works but is rather heavy handed. It is using the entire document as a query. Yes, it does match the document you are trying to Remove, but there's more data travelling over the wire than necessary.

Upvotes: 1

Related Questions