craiggwilson / fluent-mongo

Provides a fluent interface on top of the 10gen driver including Linq.
172 stars 28 forks source link

Please update to support Linq #14

Closed gyf19 closed 10 years ago

gyf19 commented 13 years ago

Please update to support Linq Collection.Update()

nekresh commented 13 years ago

I think it might be done with returning a MongoQueryable when calling AsQueryable() which will provide a custom Update/Delete method supporting linq expression.

What do you think ? Is it acceptable to hijack the AsQueryable method for doing that ? Or maybe a Fluent() extension method ?

craiggwilson commented 13 years ago

I think what he is looking for is a way to do this:

Collection.Update(x => x.LastName == "Jackson", new { FirstName = "Jim" })

UPDATE Collection SET FirstName = 'Jim' WHERE LastName = 'Jackson'

Effectively, take an Expression in the first parameter and let linq convert it into a document...

nekresh commented 13 years ago

Yep, that's what I meant.

But how do you want to modify the Update method in the mongodb collection ? That's why I propose using a custom collection class when using the AsQueryable().

MongoCollection.AsQueryable().Update(x => x.LastName == "Jackson", new { FirstName = "Jim"})

As this method will reuse a lot of fluent-mongo code, I think it's good to have it in fluent-mongo instead of reimplementing it in mongodb csharp driver. I ran into this issue recently (and with Delete as well) and might give it a quick look.

craiggwilson commented 13 years ago

Checkout the code at https://github.com/mongodb-csharp/mongodb-csharp/blob/master/source/MongoDB/LinqExtensions.cs. We did something very similiar with the old driver and the linq provider was transplanted from there to here. I assume the same thing will work.

The GetQuery method at the bottom is how to get the query document out of the IQueryable instance.