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:
- Go to http://www.mongodb.org/display/DOCS/Querying
- Get inspired by the JSON written there and either write the selector in MongoDB in plain Json-string or use the SimoJson-object.
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
Advertisement
Good article!
Thanks Daniel
I’m working on a even more simplified querying which will help you generate your queries. Hopefully I will push this out tonight.
//Daniel