Bob Horn
Bob Horn

Reputation: 34297

RavenDB Query by New Property

I just added a new property (Deleted) to an existing Foo class:

public class Foo
{
    // Other properties here
    public bool Deleted { get; set; }  // New property
}

I have 88 Foos in the DB. When I try to query by this new property, I get no documents:

session.Query<Foo>().Where(x => x.Deleted == false);

I believe this is because the Deleted property does not exist on any of the Foo documents in the DB. To get this to work, I had to get all Foos, then filter on the full list and return where Deleted == false.

session.Query<Foo>();
return foos.Where(x => x.Deleted == false);

Is this the way a change like this needs to be handled? It would be nice to just have the Where() filter in the query itself, but I can understand why that wouldn't work.

Upvotes: 3

Views: 428

Answers (2)

Bob Horn
Bob Horn

Reputation: 34297

I thought I'd post another option that was simple and easy to do.

In Raven Studio, you can execute a patch on the Patch tab. You can patch the collection so that the property now exists on every document. Just set the property equal to itself. If the property already exists, it's just set to itself. If the property doesn't exist, it will be added as null. Existing information remains; no information lost.

This is the code that was executed:

this.ApplicationWithOverrideVariableGroup.CustomVariableGroupIds = this.ApplicationWithOverrideVariableGroup.CustomVariableGroupIds;

enter image description here

Upvotes: 0

Fitzchak Yitzchaki
Fitzchak Yitzchaki

Reputation: 9163

The deleted property is not exists yet in the actual documents that stored in the database, so this is why you cannot query them based on this property.

The way to solve this, is to first add this property to the actual documents. You can do this by just load all of the documents and than save the session.

Here is a sample code how to achieve that:

public void LoadAndSave<T>()
{
    using (var session = store.OpenSession())
    {
        int skip = 0;
        const int take = 1024;
        while (true)
        {
            var builds = session.Query<T>()
                .Skip(skip)
                .Take(take)
                .ToList();
            skip += builds.Count;

            session.SaveChanges();

            if (builds.Count == 0)
                break;
        }
    }
}

Upvotes: 2

Related Questions