QuestionBank

Linq How to query a RavenDb document based on the existence of a property

Linq How to query a RavenDb document based on the existence of a property,linq,ravendb,Linq,Ravendb,Updated with exact classes & data I have a new version of a Document with a new property: public class PaperSite { public string Title { get; set; } public string Html { get; set; } public ParsedPaper ParsedPaper { get; set; } // This is a new property } I would like to query all the documents that don't have the property set (initially, all of them obviously). However, a query like this: docs.Query<PaperSite>.Where( x => x.ParsedPaper == null) returns nothing.

Updated with exact classes & data

I have a new version of a Document with a new property:

public class PaperSite
{
public string Title { get; set; }
public string Html { get; set; }
public ParsedPaper ParsedPaper { get; set; } // This is a new property
}


I would like to query all the documents that don't have the property set (initially, all of them obviously). However, a query like this:

docs.Query<PaperSite>.Where( x => x.ParsedPaper == null)


returns nothing.

Oren hinted using

contains
, so I created the following:

from site in docs.PaperSites
where site.Contains("ParsedPaper") == false
select new { PaperSite = site }


This, however returns nothing.

In the database I have the following docs (and more):

PaperSites/34 { "Title": "Paper Site Zero (v0)", "Html": null},
PaperSites/97 { "Title": "Paper Site Three", "Html": "<html></html>", "ParsedPaper": null }


The following index:

from site in docs.PaperSites
where site.ParsedPaper == null
select new { PaperSite = site }


Returns the correct docs (ones where ParsedPaper exists, but is null)

Am I doing something wrong with the Contains? How do I debug these indexes?

#1

You can't do a query like that, because what you are asking is for all documents that HAVE this property, and have it set to null.

You can do this using an index, something like:

from doc in docs
where doc.Contains("Description") == false
select new { doc.Id }


#2

Using the .Contains("...") in the where clause did not work for me either. I was able to solve my problem of finding documents with missing properties using the following index definition:

from doc in docs.MyDocType
where(doc.Description == null)
select new { doc.Id }


Subsequently I can now use this index via an UpdateByIndex to run PatchRequest scripts that fix the missing properties.

#3

That seems like a sensible solution, at least when thinking about migrations and maintaining your "schema"

#4

Oren, I could not get the 'Contains' to work. I am not sure how to debug this. Any hints?