Index and query multiple fields for nested object

41 views
Skip to first unread message

Tobbe

unread,
Feb 13, 2018, 10:18:11 AM2/13/18
to RavenDB - 2nd generation document database

Hello!
I have a document collection named Contact which contains an list of goals and the goal object looks like this:
    public class Goal
   
{
       
public string Name { get; set; }
       
public double UnixDateTime { get; set; }
   
}


Right now I'm using "Dynamic fields" to index this and it works when querying on a single property like this:

Index:
GoalsUnixTimes = contact.Goals.Select(g => CreateField("GoalsUnixTimes", g.UnixDateTime))

Query:
GoalsUnixTimes: 1517396156.0

Using the example above I get the expected result. But when trying to index and query on multiple properties I can't get it working. Check out example.png to see how it's indexed in RavenDb. The following is what I ended up trying with, without getting back any result at all.

Index:
Goals = contact.Goals.Select(g => CreateField("Goals", new { g.Name, g.UnixDateTime}))

Query:
Goals.UnixDateTime: 1517396156.0

When I get this working I want  to query on UnixDateTime and Name at the same time.

Any suggestions what I'm doing wrong here?

Regards Tobbe
Example.PNG

Oren Eini (Ayende Rahien)

unread,
Feb 15, 2018, 10:57:56 AM2/15/18
to ravendb
You can use this to do what you want:
 
GoalsUnixTimes = contact.Goals.Select(g => g.UnixDateTime)

Hibernating Rhinos Ltd  

Oren Eini l CEO Mobile: + 972-52-548-6969

Office: +972-4-622-7811 l Fax: +972-153-4-622-7811

 


--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tobbe

unread,
Feb 20, 2018, 2:28:31 AM2/20/18
to RavenDB - 2nd generation document database
Thanks for your answer.

We came up with the following solution.

Index:
__ = contact.Goals != null && Enumerable.Any(contact.Goals) ? contact.Goals.Select(p => CreateField("Goals_ + p.Name, (double)p.UnixDateTime, true, true)) : null

Query example:
string goalName = "Some goal";
query
= query.WhereBetweenOrEqual("Goals_" + goalName, dateSelection.StartDate, dateSelection.EndDate);

To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages