package com.marklogic.client.example.handson.unit04;

public class AddCollection {

}

@@ -51,36 +51,2599 @@
 4;%0A%0A
-public class AddCollection %7B%0A
+import java.io.IOException;%0A%0Aimport com.marklogic.client.DatabaseClient;%0Aimport com.marklogic.client.DatabaseClientFactory;%0Aimport com.marklogic.client.document.JSONDocumentManager;%0Aimport com.marklogic.client.example.handson.Util;%0Aimport com.marklogic.client.example.handson.Util.ExampleProperties;%0Aimport com.marklogic.client.io.DocumentMetadataHandle;%0Aimport com.marklogic.client.io.JacksonHandle;%0A/**%0A *  AddCollection illustrates how to write metadata content to a database document.%0A */%0Apublic class AddCollection %7B%0A%09public static void main(String%5B%5D args)%0A%09throws IOException %7B%0A%09%09run(Util.loadProperties());%0A%09%7D%0A%0A%09public static void run(ExampleProperties props) throws IOException %7B%0A%09%09System.out.println(%22example: %22+AddCollection.class.getName());%0A%0A%09%09// create the client%0A%09%09DatabaseClient client = DatabaseClientFactory.newClient(%0A%09%09%09%09props.host, props.port, props.adminUser, props.adminPassword,%0A%09%09%09%09props.authType);%0A%0A%09%09//Run WriteDocumentJSON again to insert the document for this example as needed%0A%09%09%0A%09%09//write metadata only for the document%0A%09    addCollection(client);%0A%09%09//write content and metadata for the document%0A%09%09addCollectionWithRead(client);%0A%0A%09%09// release the client%0A%09%09client.release();%0A%09%7D%0A%09%0A%09public static void addCollection(DatabaseClient client) %0A%09throws IOException %0A%09%7B%0A%09%09// create a manager for JSON documents%0A%09%09JSONDocumentManager docMgr = client.newJSONDocumentManager();%0A%0A%09%09// create an identifier for the document%0A%09%09String docId = %22/image/20140721_144421b.jpg.json%22;%0A%0A%09%09// create and initialize a handle on the metadata%0A%09%09DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle();%0A%09%09metadataHandle.getCollections().addAll(%22phone%22);%0A%0A%09%09// write the document metadata%0A%09%09docMgr.writeMetadata(docId, metadataHandle);%0A%0A%09%09System.out.println(%22Added %22+docId+%22 to the phone collection.%22);%0A%09%7D%0A%09%0A%09public static void addCollectionWithRead(DatabaseClient client) %0A%09throws IOException %0A%09%7B%0A%09%09// create a manager for JSON documents%0A%09%09JSONDocumentManager docMgr = client.newJSONDocumentManager();%0A%0A%09%09// create an identifier for the document%0A%09%09String docId = %22/image/20140721_144421b.jpg.json%22;%0A%09%0A%09%09//read the document as JSON%0A%09%09JacksonHandle readHandle = new JacksonHandle();%0A%09%09docMgr.read(docId, readHandle);%0A%0A%09%09// create and initialize a handle on the metadata%0A%09%09DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle();%0A%09%09metadataHandle.getCollections().addAll(%22phone%22);%0A%0A%09%09// write the document metadata and content%0A%09%09docMgr.write(docId, metadataHandle, readHandle);%0A%0A%09%09System.out.println(%22Wrote content and added %22+docId+%22 to the phone collection.%22);%0A%09%7D%0A%09
 %0A%7D%0A

add document to a collection example