Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for $in where the property is an array #22

Open
gabrielduford opened this issue Aug 25, 2011 · 4 comments
Open

Add support for $in where the property is an array #22

gabrielduford opened this issue Aug 25, 2011 · 4 comments
Labels

Comments

@gabrielduford
Copy link

Hi,
I'd like to implement the following using FluentMongo (this is a LINQPad query):

void Main() 
{
    MongoServer mongo = MongoServer.Create(new MongoConnectionStringBuilder() { Server = new MongoServerAddress("localhost") } );
    MongoDatabase database = mongo["Demo"];
    MongoCollection<Employee> employeeCollection = database.GetCollection<Employee>("Employees");

    Employee emp1 = new Employee() { Name = "Bob" };
    emp1.Departments.Add("Dep1");
    emp1.Departments.Add("Dep2");

    Employee emp2 = new Employee() { Name = "Binette" };
    emp2.Departments.Add("Dep2");
    emp2.Departments.Add("Dep3");

    employeeCollection.InsertBatch(new[] { emp1, emp2 });

    IEnumerable<Employee> searchResult =
        employeeCollection
        .Find(Query.In("Departments", BsonArray.Create(new [] { "Dep1", "Dep2" })));

    searchResult.Dump(); // LINQPad dumps the result to the output.

    employeeCollection.RemoveAll();
}

class Employee
{
    public Employee()
    {
        Departments = new List<string>();
    }

    public ObjectId Id { get; set; }
    public string Name { get; set; }
    public List<string> Departments
    { 
        get; 
        private set; 
    }
}

Intuitively, I would implement the query like this in FluentMongo, but I get a NotSupportException:
IEnumerable searchResult2 =
employeeCollection
.AsQueryable()
.Where(o => o.Departments.Intersect(new [] { "Dep1", "Dep2" }).Any());

Is there a better way to implement this kind of query in FluentMongo?
Thanks,

gabriel

@gabrielduford
Copy link
Author

Documentation for what I am trying to achieve:
http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-ValueinanArray

gabriel

@ghost
Copy link

ghost commented Sep 29, 2011

I would also like to request the same feature, or if otherwise possible would like to know how this can be achieved using fluent mongo?

@craiggwilson
Copy link
Owner

@gabrielduford
Copy link
Author

Hummm... The case is not the same. My case is about supporting "Enumerable.Intersect".

@craiggwilson craiggwilson reopened this Nov 22, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants