Litedb: Query against child properties

Created on 25 Jul 2016  路  6Comments  路  Source: mbdavid/LiteDB

I have a class Post which is stored in the collection "post". Each Post has a nullable "Tags" property which is List. Tag has a property "Name". How can I query my posts where a given tag has a name of "mytag" ?

I tried

posts.Find(p => p.Tags.Any(t => t.Name == "mytag"))

and I also tried

posts.Find(Query.In("Tags.Tag.Name", "mytag"))

both return System.InvalidOperationException. The former with "variable 'p' of type 'Post' referenced from scope '', but it is not defined" and the latter with "sequence contains no elements"

I tried a few other options:

.Find(p => p.Tags != null && p.Tags.Any() && p.Tags.Any(t => t.Name == "mytag")) -- "Index was out of range. Must be non-negative and less than the size of the collection"

.Find(p => p.Tags != null && p.Tags.Count() > 0 && p.Tags.Any(t => t.Name == "mytag")) -- " Invalid type owner for DynamicMethod"

Am I missing something obvious or is this just not supported?

question

Most helpful comment

Hi @Still34, if you are using v3/v4 it's possible works with MultiKey Index. You can see here: https://github.com/mbdavid/LiteDB/issues/474#issuecomment-279091158 or here https://github.com/mbdavid/LiteDB/issues/915

All 6 comments

Hi @WaywardMage, this are a missing feature in LiteDB. You need is a "Multikey index" (see https://docs.mongodb.com/v3.0/core/index-multikey/).

Document store databases has many kinds of indexes: (see https://docs.mongodb.com/v3.0/core/index-types/). LiteDB, in current version, support only most used one: "Single Field Index".

And with my current engine it麓s not possible implement. Is on my plans, no next major version, re-write all engine system just because this: multikey indexes and compound indexes).

So, for this operation you will need use a non-indexed query, like:

.FindAll().Where(p => p.Tags != null && p.Tags.Any(t => t.Name == "mytag"));

That worked. Thanks for the tip!

Hi @WaywardMage/ @mbdavid
are you (or another contributor) working on a multi-key index implementation? this project looks very cool and usable, however this querying into indexed child object lists is a must for must use cases (get customers with specified tags, get orders with specified products). cheers!

Hi @vip32, I'm working on it on dev-mk branch. I believe that will be present in v3 version.

@mbdavid Is this addressed? Because it still throws for me with a similar situation here in the latest version.

Hi @Still34, if you are using v3/v4 it's possible works with MultiKey Index. You can see here: https://github.com/mbdavid/LiteDB/issues/474#issuecomment-279091158 or here https://github.com/mbdavid/LiteDB/issues/915

Was this page helpful?
0 / 5 - 0 ratings