Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to query RavenDB using complex query in F#

Tags:

nosql

f#

ravendb

I am using RavenDB 2.0 unstable. Is it possible to query RavenDB 2.0 with a lambda or similar technique (linq?) from F#?

Here is intent example code. I am attempting to get all the posts in last 72 hours. Its wrong, but it gets the idea across.

static member public GetTopPosts _ =
    use store = (new DocumentStore(Url = Docs.serverUrl, DefaultDatabase = Docs.dbName )).Initialize()
    use session = store.OpenSession()
    let result = session.Query<Post>().Where(fun x -> x.Date > DateTime.Now - new TimeSpan(72,0,0))
    ...

How can something like this be accomplished?

like image 911
enko Avatar asked Jan 30 '26 11:01

enko


1 Answers

Yes you can do this.

You can find an example of this in FAKE

In your case your query would look something like this

let getTopPostsAsOf date =
    use session = docStore.OpenSession()
    query {
       for post in session.Query<Post>() do
       where (post.Date > date)
       select post
    } |> Seq.toArray

this would then be called with

let result = getTopPostsAsOf (DateTime.Now - TimeSpan.FromHours(72))

A couple of things to note.

  1. The DocumentStore should be created once and only once at the startup of the application.

  2. Also typically only a small set of library calls can be used within the query expressions, hence why I pass in the already computed date. The reason for this is because these expression trees end up getting serialized and executed within raven db engine.

like image 77
Colin Bull Avatar answered Feb 02 '26 02:02

Colin Bull