[RavenDB] Querying DateTime

1,500 views
Skip to first unread message

Tobi

unread,
May 20, 2010, 10:47:10 AM5/20/10
to ravendb
Hi!

Having documents with a DateTime field, how do I query them by their
time field?

The Linq provider creates a query like "Time:{01.12.2009 00:00:00 TO
*}", which Lucene doesn't like. But I couldn't figure this out manually
either.

Tobias


Ayende Rahien

unread,
May 20, 2010, 11:25:32 AM5/20/10
to ravendb
The datetime format is a bit freeky.
Same as the integers, by the way.
Gimme a few hours, I'll send you the fix.

As for the format look at DateUtls.DateToString

Tobi

unread,
May 20, 2010, 11:34:32 AM5/20/10
to rav...@googlegroups.com
Am 20.05.2010 17:25, schrieb Ayende Rahien:

> The datetime format is a bit freeky.
> Same as the integers, by the way.
> Gimme a few hours, I'll send you the fix.

No need to hurry.

> As for the format look at DateUtls.DateToString

I've already tried this, but the query didn't return any result.
Maybe I did something wrong with the index?

documentStore.DatabaseCommands.PutIndex("Sale.Time", new
IndexDefinition<Sale>()
{
Map = docs => from doc in docs
select new
{
doc.Time
}
});

Tobias

Nathan Palmer

unread,
May 20, 2010, 9:02:05 PM5/20/10
to rav...@googlegroups.com
I have a similar question as I've been fighting with datetime values
in both RavenDB and MongoDB. What I'd like to do is get a result of
all the documents that have been updated in the last 15 minutes. So
far to accomplish this I have a document that looks like this

{
"UserName":"snowman",
"LastActivityDate":"/Date(1274403556254)/"
}

I couldn't see a way to create an index that could work with the date
at all.. or even compare to some type of a system date. I have also
toyed around with the idea of not treating it as a date but as an
integer or a string. But it would be nice to keep it functional as a
date.. at least it makes it nicer to work with in C#.

Nathan Palmer

Ayende Rahien

unread,
May 21, 2010, 3:37:08 AM5/21/10
to rav...@googlegroups.com
This should now just work. I'll have a longer discussion on how we are doing the indexing for those now shortly.

Ayende Rahien

unread,
May 21, 2010, 3:38:59 AM5/21/10
to rav...@googlegroups.com
Using the latest:

// index definition
from docs in docs.Users
select new {LastActivityDate};

// query def at the client

var activeDate = DateTime.Now.AddMinutes(-15);
var activeUsers = from user in session.Query<User>("Users/ByActivity")
                          where user.LastActivityDate > activeDate
                          select user;

This should do it.

Nathan Palmer

unread,
May 22, 2010, 5:18:21 PM5/22/10
to rav...@googlegroups.com
Seems pretty straight forward. I tried it on build 74 (and a few
previous ones) and this is what i get.

After entering 1 user document into the database and creating the
index. I run these two lines.

List<User> result1 = session.LuceneQuery<User>("Users/ByActivity").ToList();
List<User> result2 = session.Query<User>("Users/ByActivity").ToList();

I previously had the where on there query but took it off to try and
break it down to it's first failing moment. The LuceneQuery comes back
with a result. The Query gives a NullReferenceException with this
stacktrace.

at Raven.Client.Linq.RavenQueryProvider`1.Execute(Expression expression)
at Raven.Client.Linq.RavenQueryProvider`1.System.Linq.IQueryProvider.Execute(Expression
expression)
at Raven.Client.Linq.RavenQueryable`1.GetEnumerator()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at TestRavenDBDate.Program.Main(String[] args) in c:\documents and
settings\nathan\my documents\visual studio
2010\Projects\TestRavenDBDate\TestRavenDBDate\Program.cs:line 34
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly,
String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state, Boolean
ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

Nathan

Ayende Rahien

unread,
May 22, 2010, 5:20:04 PM5/22/10
to rav...@googlegroups.com
*snort*
The linq impl doesn't implement the null scenario :-)

Ayende Rahien

unread,
May 23, 2010, 4:19:46 AM5/23/10
to rav...@googlegroups.com
This should work now.

Chris Marisic

unread,
Sep 29, 2011, 5:16:47 PM9/29/11
to rav...@googlegroups.com
Where is DateUtls.DateToString? I can't seem to find it. Was this renamed/refactored etc?

I'm trying to query using lucene where(string) and need to compound in a query that has a datetime.

Matt Warren

unread,
Sep 30, 2011, 6:21:01 AM9/30/11
to ravendb
It got renamed to "DateTools.DateToString" and it lives in :
namespace Raven.Abstractions.Linq
Reply all
Reply to author
Forward
0 new messages