Search code examples
c#ravendbravendb-studio

RavenDB query doesn't work from C# but it works in RavenDB Studio?


Actually I want to delete all documents of a kind in a test procedure but I couldn't do that using DocumentStore.DatabaseCommands.DeleteByIndex command , so I tried to see if I could query those entities , Here's my code :

var store = new DocumentStore { Url = "http://localhost:8080" };
            store.Initialize();
            var result= store.DatabaseCommands.Query("Raven/DocumentsByEntityName",
                new IndexQuery { Query = "Tag : RunningTables" },null);
            result.Results.ForEach(x=>x.WriteTo(new JsonTextWriter(Console.Out)));

It returns no document but when I use RavenDB Studio and execute the same query on Raven/DocumentsByEntityName index.

Then I took a look at my command Url and I realized start=30 so I changed the code as follow :

var result= store.DatabaseCommands.Query("Raven/DocumentsByEntityName",
                    new IndexQuery { Query = "Tag : RunningTables",Start=0 },null);

But nothing changed except that now Url doesn't contain start anymore. What's wrong with my code ?


Solution

  • OK. I've got what was wrong with my Code.I didn't select the default database and there was more than one database in my RavenDB. So I did it like this :

    var store = new DocumentStore { Url = "http://localhost:8080" , DefaultDatabase = "MyDbName" };
                store.Initialize();
                var result= store.DatabaseCommands.Query("Raven/DocumentsByEntityName",
                    new IndexQuery { Query = "Tag : RunningTables" },null);
                result.Results.ForEach(x=>x.WriteTo(new JsonTextWriter(Console.Out)));