Michał Wcisło
Michał Wcisło

Reputation: 43

RavenDB querying metadata

I want to prevent documents from being deleted in my project and I decided to use metadata to mark document as Archived. I used below code to do that:

public class DeleteDocumentListener : IDocumentDeleteListener
{
    public void BeforeDelete(string key, object entityInstance, RavenJObject metadata)
    {
        metadata.Add("Archived", true);
        throw new NotSupportedException();
    }
}

After that I wanted to alter query to return only documents which have Archived metadata value set to false:

using (var session = _store.OpenSession())
{
   var query = session.Advanced.DocumentQuery<Cutter>()
                .WhereEquals("@metadata.Archived", false);
}

Unfortunately this query return empty result set. It occurs that if Document doesn't have this metadata property then above condition is treated as false. It wasn't what I expected.

How can I compose query to return Documents which don't have metadata property or this property has some value ?

Upvotes: 2

Views: 1083

Answers (1)

Jens Pettersson
Jens Pettersson

Reputation: 1177

You can solve it by creating an index for you Cutter documents and then query against that:

public class ArchivedIndex : AbstractIndexCreationTask<Cutter>
{
    public class QueryModel
    {
        public bool Archived { get; set; }
    }

    public ArchivedIndex()
    {
        Map = documents => from doc in documents
            select new QueryModel
            {
                Archived = MetadataFor(doc)["Archived"] != null && MetadataFor(doc).Value<bool>("Archived")
            };
    }
}

Then query it like this:

using (var session = documentStore.OpenSession())
{
    var cutters = session.Query<ArchivedIndex.QueryModel, ArchivedIndex>()
        .Where(x => x.Archived == false)
        .OfType<Cutter>()
        .ToList();
}

Hope this helps!

Quick side note. To create the index, the following code may need to be run:

new ArchivedIndex().Execute(session.Advanced.DocumentStore);

Upvotes: 5

Related Questions