Welcome Guest, you are in: Login

dbones docs

RSS RSS

Navigation (ArmChair)





Search the wiki
»

Querying

RSS
Modified on 2017/03/19 13:19 by Dave Categorized as Uncategorized

Overview

This article covers the basics for querying directly with ArmChair.

Query types

Armchair was initially designed to work with an Index service such as Elastic, Solr or Lucene, with the latest version of CouchDB, Armchair has support for Querying directly against CouchDB.

2 methods for querying are supported by Armchair (starting at 0.5.x-alpha), both are built ontop of the Mongo Query feature (requires Couchdb 2.0)

  • Mongo query - exposes the mongo query directly.
  • LINQ qurery - Linq to mongo support (note that Armchair and Mongo Queries does not support all the functionality of Linq)

Why use one over the other?

Use Mongo when

  • you require full control over the mongo query.
  • access to all the mongo query features.

Use Linq when you require

  • strong typing, refactoring support and intellisence
  • simpler user experience.

Mongo

The Mongo query accepts a MongoQuery object, this object has all the required properties, such as paging, sorting and index etc.

the following will return all documents which have a property called editions containing an element with a name equal to "2nd".


using (var session = Database.CreateSession())
{
    var json =  "{ \n" +
                "    \"editions\": { \n" +
                "        \"$elemMatch\": { \n" +
                "            \"name\": { \n" +
                "                \"$eq\": \"2nd\" \n" +
                "            } \n" +
                "        } \n" +
                "    } \n" +
                "} \n";

    //note the camel case. (as we do no use the IDictionary at each level on the deseialise)
    var query = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);

    var results = session.Query<object>(new MongoQuery()
    {
        Selector = query
    });
}

Linq for Mongo

NOTE:
to use the linq query you will need to tell Armchair about all the types, at the bottom of this segment we have a simple way to do this.

The linq interface converts linq into a MongoQuery object.

the example below retrieves all the books with an edition called "2nd".


using (var session = Database.CreateSession())
{
    var results = session.Query<Book>().Where(x => x.Editions.Any(y=> y.Name == "2nd"));
}

as mentioned we need to register our persistable types, this is to allow us to do polymorphic queries,

ie session.Query < Person > () would return objects of type Person or which inherit Person.


//find all the types we perist
var types = typeof(Person).Assembly.GetTypes().Where(x => typeof(EntityRoot).IsAssignableFrom(x)).ToList();
//register them with Armchair
Database.Register(types);
dbones docs, is a sub-site of dbones.co.uk, all its content belongs to dbones.co.uk, this also includes logo's.