Working with documents
Creating a document
Now we create a document in the collection. Any object can be added as a document to the database and be retrieved from the database as an object.
For this example, we use the class BaseDocument, provided with the driver. The attributes of the document are stored in a map as key/value pair (String/Object):
String key = "myKey";
BaseDocument doc = new BaseDocument(key);
doc.addAttribute("a", "Foo");
doc.addAttribute("b", 42);
System.out.println("Inserting document...");
collection.insertDocument(doc);
Some details you should know about the code:
- the document key is passed to the
BaseDocument
constructor addAttribute()
puts a new key/value pair into the document- each attribute is stored as a single key-value pair in the document root
Create a document from Jackson JsonNode
We can also create a document from a Jackson JsonNode object:
System.out.println("Creating a document from Jackson JsonNode...");
String keyJackson = "myJacksonKey";
JsonNode jsonNode = JsonNodeFactory.instance.objectNode()
.put("_key", keyJackson)
.put("a", "Bar")
.put("b", 53);
System.out.println("Inserting document from Jackson JsonNode...");
collection.insertDocument(jsonNode);
Create a document from a JSON String
Documents can also be created from raw JSON strings:
System.out.println("Creating a document from JSON String...");
String keyJson = "myJsonKey";
RawJson json = RawJson.of("{\"_key\":\"" + keyJson + "\",\"a\":\"Baz\",\"b\":64}");
System.out.println("Inserting document from JSON String...");
collection.insertDocument(json);
Read a document
To read the created document:
System.out.println("Reading document...");
BaseDocument readDocument = collection.getDocument(key, BaseDocument.class);
System.out.println("Key: " + readDocument.getKey());
System.out.println("Attribute a: " + readDocument.getAttribute("a"));
System.out.println("Attribute b: " + readDocument.getAttribute("b"));
After executing this program the console output should be:
Key: myKey
Attribute a: Foo
Attribute b: 42
Read a document as Jackson JsonNode
Documents can also be read as Jackson JsonNode:
System.out.println("Reading document as Jackson JsonNode...");
JsonNode readJsonNode = collection.getDocument(keyJackson, JsonNode.class);
System.out.println("Key: " + readJsonNode.get("_key").textValue());
System.out.println("Attribute a: " + readJsonNode.get("a").textValue());
System.out.println("Attribute b: " + readJsonNode.get("b").intValue());
After executing this program the console output should be:
Key: myKey
Attribute a: Bar
Attribute b: 53
Some details you should know about the code:
getDocument()
returns the stored document as instance ofcom.fasterxml.jackson.databind.JsonNode
.
Read a document as JSON String
Documents can also be read as raw JSON strings:
System.out.println("Reading document as JSON String...");
RawJson readJson = collection.getDocument(keyJson, RawJson.class);
System.out.println(readJson.getValue());
After executing this program the console output should be:
{"_key":"myJsonKey","_id":"firstCollection/myJsonKey","_rev":"_e0nEe2y---","a":"Baz","b":64}
Update a document
Let’s update the document:
doc.addAttribute("c", "Bar");
System.out.println("Updating document ...");
collection.updateDocument(key, doc);
Let’s read the document again:
System.out.println("Reading updated document ...");
BaseDocument updatedDocument = collection.getDocument(key, BaseDocument.class);
System.out.println("Key: " + updatedDocument.getKey());
System.out.println("Attribute a: " + updatedDocument.getAttribute("a"));
System.out.println("Attribute b: " + updatedDocument.getAttribute("b"));
System.out.println("Attribute c: " + updatedDocument.getAttribute("c"));
After executing this program the console output should look like this:
Key: myKey
Attribute a: Foo
Attribute b: 42
Attribute c: Bar
Delete/Remove a document
Let’s delete a document:
System.out.println("Deleting document ..."); collection.deleteDocument(key);
Anything unclear or buggy in this tutorial? Provide Feedback