开发者

RavenDb store and retrieve class-less objects (json)

开发者 https://www.devze.com 2023-04-04 08:16 出处:网络
I\'m writing a application where the user can write json-code and store that json code with an Id and a Collection. In other words, they specify an Id, a Collection (string; [a-zA-Z0-9]) and a Data (j

I'm writing a application where the user can write json-code and store that json code with an Id and a Collection. In other words, they specify an Id, a Collection (string; [a-zA-Z0-9]) and a Data (json, can be anything that is valid json).

Up til now I've been using RavenDb for this, cause I thought a document-database would be perfect, but I've had some problems with querying.

One of the objects that needs to be stored and queried is the following:

{
    "network": "some_network",
    "names": ["name1","name2"],
    "data": {"val开发者_Python百科ues":[],"keys":[]}
}

This object should be stored with some Id that is either specified, or auto-generated (if null is given), and a Collection (must always be specified), and then I need to be able to query it based on Collection, network and a single name.

For instance, I have the code query('users', '{"network":"some_network","names":"name1"}'), and I need that code to return this object (and any other object that matches it).

Also, I'm ok with changing database, but the database needs to be able to run in-process (self-hosted), and to be able to run without admin-rights without installation (in other words, it can't bind to hostname/ip like wcf does).

How can I achieve something like this?


I found the answer to this:

    public string Query(string dynIndexName, string query)
    {
        using (var session = store.OpenSession())
        {
            var q = new IndexQuery();
            q.Query = query;
            var result = session.Advanced.DatabaseCommands.Query("dynamic/" + dynIndexName, q, new string[0]);
            return "[" + String.Join(",", result.Results.Select(r => r.ToString())) + "]";
        }
    }

Before calling the Query-method I convert the json-query-object into a Lucene-query that looks like this: (network:"some_network" AND names:"name1"), and use that as a query-parameter to the database. The whole class for storing and retrieving can be found here: https://github.com/Alxandr/RunJS/blob/master/src/AddIns/Storage/StorageMbro.cs

0

精彩评论

暂无评论...
验证码 换一张
取 消