craiggwilson / fluent-mongo

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

StringArray.Contains( "string" ) translated in { StringArray : [ 's' , 't' , 'r' , 'i' , 'n' , 'g' ] } #43

Closed gautiers closed 12 years ago

gautiers commented 12 years ago

Hello,

I have a Contact class

public class Contact
{
    private string _firstname;
    private string _lastname;
    private string _company;

    private string [] _searches;

    ....

    [corresponding properties]
 }

_searches array contains lower case / without accent version of _firstname, _lastname, _company as items

So I would like to do : AsQueryable().Where( c => c.Searches.Contains( mySearch ) )

And i think it should be translated in something like { "Searches" : mySearch } }

But in the log, i have : { Searches: [ 103, 97, 117, 116, 105, 101, 114 ] } where the numbers are the ascii code of each letter of "mySearch" (the real search was "gautier")

Same thing with AsQueryable().Where( c => c.Searches.Any( s => s == mySearch ) )

Am I doing something wrong here ?

Next step would be to use AsQueryable().Where( c => c.Searches.Any( s => s.StartsWith(mySearch) ) ) Here it tells me that StartsWith has to be used on the mongo field But i think it is. Or am i missing something ?

Thank you

gautiers commented 12 years ago

I tried with the version 1.3.1 and I have the same behavior

craiggwilson commented 12 years ago

Yeah. I haven't had a chance to look at this yet. I'll try and get to it in the next couple days.

gautiers commented 12 years ago

The problem does not occur if the collection is of type ICollection<string> instead of string [] So this works : AsQueryable().Where( c => c.Searches.Contains( mySearch ) )

But this : AsQueryable().Where( c => c.Searches.Any( s => s.StartsWith(mySearch) ) ) still does not work, it says that "The mongo field must be the operator for a string operation of type StartsWith" The corresponding MongoQuery would be : { "Searches" : /mySear*/ }

craiggwilson commented 12 years ago

This should be fixed in release 1.3.1.1.

paulb2011 commented 12 years ago

Using 1.3.11

AsQueryable().Where( m => m.Name.ToUpper().StartsWith( name.ToUpper() ) );

I get InvalidQueryException, The mongo field must be the operator for a string operation of type StartsWith

craiggwilson commented 12 years ago

Correct, you can only call StartsWith on a field. There is no way in mongo to uppercase a field at the server side...

However, you can perform a case-insensitive regex search instead... Regex.IsMatch(m.Name, name, RegexOptions.CaseInsensitive)... or something like that.