Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store JSON to RavenDB?

I want to take existing data and put it into RavenDB.

My existing data was in an XML format, so I converted it to JSON.

What should my next step be? Can I store it in RavenDB as is? Do I need to create new objects to store it?

Thanks in advance!

like image 829
ZacAttack Avatar asked Nov 09 '11 21:11

ZacAttack


2 Answers

It is not mandatory to submit content to RavenDB using the RavenDB Client, nor is it necessary to populate a domain model first. This is unnecessary effort and only complicates the process of data submission/insertion/migration/import.

You can submit JSON formatted documents directly to RavenDB using the HTTP API, specifically you may wish to review the "Single Document Operations" topic for simple examples which (currently) show examples using 'curl'.

Consider the following .NET code:

var url = string.Format("http://ravendb-server:8080/databases/{0}/docs/{1}", databaseName, docId);
var webRequest = System.Net.HttpWebRequest.CreateHttp(url);
webRequest.Method = "PUT";
webRequest.ContentType = "application/json";
webRequest.Headers["Raven-Entity-Name"] = entityName;
var stream = webRequest.GetRequestStream();
using (var writer = new System.IO.StreamWriter(webRequest.GetRequestStream()))
{
    writer.Write(json);
}
var webResponse = webRequest.GetResponse();
webResponse.Close();

The above snippet allows you to submit a valid JSON document into a specific database and a specific document collection with the specified ID. Database selection and ID designation is performed through URL paths, and the Document Collection is specified with the metadata header Raven-Entity-Name.

There are additional metadata headers you may want to send up, such as Raven-Clr-Type or Last-Modified but they are not required.

like image 94
Shaun Wilson Avatar answered Oct 19 '22 19:10

Shaun Wilson


I suppose that your json-data represents the data of your applications domain, and you want to have classes with properties to work with that data in your application, right?

If that is the case, you need to write a simple import-application, that populates your domain model once and then stores all your objects as regular RavenDB documents, just the way you would store any other object with RavenDB.

Does that make sense?

like image 31
Daniel Lang Avatar answered Oct 19 '22 19:10

Daniel Lang