legion
legion

Reputation: 497

How to extract property of a collection in the root document

I'm using RavenDB and I'm having trouble extracting a particular value using the Lucene Query.

Here is the JSON in my document:

{
 "customer" : "my customer"
 "locations": [
    {
      "name": "vel arcu. Curabitur",
      "settings": {
         "enabled": true
      }
    }
  ]
}

Here is my query:

var list = session.Advanced.LuceneQuery<ExpandoObject>()
   .SelectFields<ExpandoObject>("customer", "locations;settings.enabled", "locations;name")
   .ToList();

The list is populated and contains a bunch of ExpandoObjects with customer properties but I can't for the life of me get the location -> name or location -> settings -> enabled to come back.

Is the ";" or "." incorrect usage??

Upvotes: 1

Views: 331

Answers (2)

Ayende Rahien
Ayende Rahien

Reputation: 22956

Technically, you can use the comma operator "," to nest into collections. That should work, but it isn't recommended. You can just get your whole object and use it, it is easier and faster.

Upvotes: 0

Daniel Lang
Daniel Lang

Reputation: 6839

It seems that you have misunderstood the concept of indexes and queries in RavenDB. When you load a document in RavenDB you always load the whole document including all of its contents it contains. So in your case, if you load a customer, you already have the collection and all its children loaded. That means, you can use standard linq-to-objects to extract all these values, no need for anything special like indexes or lucene here.

If you want to do this extraction on the database side, so that you can query on those properties, then you need an index. Indexes are written using linq, but it's important to understand that they run on the server and just extract some data to populate the lucene index from. But here again, in most cases you don't even have to write the indexes yourself because RavenDB can create them automatically for you.

I no case, you need to write lucene queries like the one in your question because in RavenDB lucene queries will always be executed against a pre-built index, and these are generally flat. But again, chances are you don't need to do anything with lucene to get what you want.

I hope that makes sense for you. If not, please update your question and tell us more about what you actually want to do.

Upvotes: 1

Related Questions