Delete documents in a batch by expiration date in ravenDB

I am trying to delete documents older than a certain point in ravenDB. I am still not familiar with Lucene and it is difficult for me to fulfill this request.

        _Session.Advanced.DatabaseCommands.DeleteByIndex(
            typeof(AssetsByExpirationDate).Name,
            new IndexQuery()
            {
                Query = string.Format("ExpirationDate:[\"{0:MM/dd/yyyy}\" TO \"{1:MM/dd/yyyy}\"]", DateTime.MinValue, new DateTime(2012,6,1))
            });

What is the correct query syntax for removing items before a specific date?

+3
source share
1 answer

You can create your query outside of DatabaseCommands IndexQuery and use Query.ToString () to populate the Query IndexQuery string, as shown below:

        var query = session.Advanced.LuceneQuery<Asset, AssetsByExpirationDate>()
            .WhereBetween("ExpirationDate",DateTime.MinValue,new DateTime(2012, 6, 1));

        var queryString = query.ToString();

        session.Advanced.DatabaseCommands.DeleteByIndex(typeof(AssetsByExpirationDate).Name, new IndexQuery
        {
            Query = queryString
        });

Using this method, and if you are not very familiar with the lucene query syntax, the RavenDb Query API will build it for you as shown above by calling .ToString () and getting the next query string in Lucene format.

ExpirationDate:{00010101000000000 TO 20120601000000000}

, DatabaseCommands . lucene , Session.Delete() foreach.

        var query = session.Advanced.LuceneQuery<Asset, AssetsByExpirationDate>()
            .WhereBetween("ExpirationDate",DateTime.MinValue,new DateTime(2012, 6, 1));
        var assets = query.ToList();

        foreach(var asset in assets)
        {
            session.Delete<Asset>(asset);
        }

        session.SaveChanges();

, Ravendb 128 .

+5

All Articles