Search code examples
c#subdocumentlitedb

In LiteDB how do I Query a subdocument?


Given the following Person Class document and its BookBag[] subdocument Array:

public class Person
{
  public string Name { get; set; }
  public string LastName { get; set; }
  public BookBag[] Bags { get; set; }
  
  public class BookBag
  {
    //contains books of various titles
    public string[] Content { get; set; }
  }
}

How do I get all Persons that have a BookBag with a book title string that contains the word "Apple" in LiteDB?

I've tried the following statement in my using database block, but failed:

col.Find("SUBSTRING($.Bags[*].Content, 0, 1) = 'Apple'")


Solution

  • Oh I just figured out my own dilemma. The correct statement is:

    col.Find("$.Bags[*].Content[*] ANY LIKE '%Apple%'");

    Please correct me if there is a better way to do this.