Question

I'm new to RavenDB and I have a Raven DB document:

Student
{
  Id : int
  Subjects : List<int>
}

I'm trying to write a query to get a intersection of the subjects of student with id 1 and student with id 2

{
  ID : 1
  Subjects : {22, 23, 25}
}

{
  ID : 2
  Subjects : {22, 25 }
}

The intersection these would be {22,25} I also need the count of the intersection subjects 2 in this case.

What's the best way to approach this type of query? Are there any other NoSQL solutions that handle this kind of query better? Also, I'm trying to cache the student collection in memory.

I need a db that supports sharding, and also I have a dataset of 15 million documents (I can shard them across different machines with a db solution like Raven or Mongo). I have to do this at db level, and I couldn't find anything how to do this at db level in the RavenDB documentation.

Was it helpful?

Solution

Based on your comments (which was not your original question), you can perform the following query:

var q = session.Query<Student>()
               .Where(x => x.Subjects.Any(y => y == 22))
               .Intersect()
               .Where(x => x.Subjects.Any(y => y == 25));

The equivalent Lucene query would be:

Subjects:22 INTERSECT Subjects:25

Given this data:

Student { Id = 1, Subjects = new List<int> { 22, 23, 25 } }
Student { Id = 2, Subjects = new List<int> { 22, 25 } }
Student { Id = 3, Subjects = new List<int> { 23, 25} }
Student { Id = 4, Subjects = new List<int> { 22 } }

Only students 1 and 2 will be returned, because 3 and 4 do not have both values.

You can read more about Intersection Queries in the documentation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top