Simple-MongoDB – Querying

Updated: Read more about Simplified Querying

So I keep getting questions about how to write queries in Simple-MongoDB. I will try to get time documenting this, but until then, and since I relly on JSON, you could attack the problem like this:

To take an example, today I got a question about how to handle OR queries. If you have a look at the documentation (http://www.mongodb.org/display/DOCS/OR+operations+in+query+expressions) you find that it isn’t implemented in v1.4 but you can use the $where-operator.

$where-operator – Using JSON

var persons = entityStore.Find<Person>(@"{$where : ""this.Name == 'Daniel' || this.Name == 'Sue'""}");

or

var persons = session[DbName][PersonsCollectionName].Find<Person>(@"{$where : ""this.Name == 'Daniel' || this.Name == 'Sue'""}");

$where-operator – Using the Simo-WhereOperator

var persons = entityStore.Find<Person>(new WhereOperator(@"this.Name == 'Daniel' || this.Name == 'Sue'"));

or

var persons = session[DbName][PersonsCollectionName].Find<Person>(new WhereOperator(@"this.Name == 'Daniel' || this.Name == 'Sue'"));

$in-operator – Using JSON

var persons = entityStore.Find<Person>(@"{Name : { $in : [""Daniel"", ""Sue""] } }");

or

var persons = session[DbName][PersonsCollectionName].Find<Person>(@"{Name : { $in : [""Daniel"", ""Sue""] } }");

$in-operator – Using the Simo-InOperator

var persons = entityStore.Find<Person>(new InOperator("Name", "Daniel", "Sue"));

or

var persons = session[DbName][PersonsCollectionName].Find<Person>(new InOperator("Name", "Daniel", "Sue"));

The code can be aquired at the Google-code, project site.

//Daniel

2 thoughts on “Simple-MongoDB – Querying

    • I’m working on a even more simplified querying which will help you generate your queries. Hopefully I will push this out tonight.

      //Daniel

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s