Skip to main content

Using Document Database with .NET

The InterSystems IRIS® Document Database driver for .NET (Document.Document) is an interface to Document Database on the server that enable you to work with Document Databases, collections, and individual documents.

Connecting to the Database

This section demonstrates DataSource method CreateDataSource() and Collection methods GetCollection(), Drop(), Size(), GetAll()

Get a Database Handle: CreateDataSource()

The database handle is the entry point for using the document data model in all client driver implementations. In .NET, the database handle is a DataSource:

DataSource datasrc = DataSource.CreateDataSource();
datasrc.SetConnectionString(
   "Server = 127.0.0.1;
   Port=1972;
   Namespace=USER;
   Password = SYS;
   User ID = _SYSTEM;"
);
Connection connection = datasrc.GetConnection();

.NET Collections

This section demonstrates Collection methods GetCollection(), Drop(), Ssize(), GetAll()

Get a Collection: GetCollection()

A collection is a container for Document Database documents. It is the primary interface for working with a Document Database. You do not have to explicitly create a collection. GetCollection() creates the specified collection if that collection does not yet exist:

Collection collectedDocs = Collection.GetCollection(datasrc,"collectionName");
Count Documents in a Collection: Size()

To count the number of documents with a collection instance invoke the Size() method:

Console.Writeln("Size = " + collectedDocs.Size());
Iterate through the Documents in a Collection: GetAll()

To iterate through all of the documents in a collection, invoke the GetAll() method, then sequence through that iterator:

DocumentList allDocuments = collectedDocs.GetAll();
allDocuments.ForEach(document=>
   Console.Out.WriteLine(Document.ToJSONString(document)));

Drop a Collection: Drop()

Dropping a collection will delete the extent of the collection and its metadata, except index definitions. This deletes all documents in the collection, but does not delete the collection itself. Any object references to the collection will stay valid and you can continue to work with it (e.g. store new documents). To drop (delete) a collection invoke the Drop() method:

collectedDocs.Drop();

.NET Documents and Transactions (Add/Create/Insert/Delete)

Document.Document class serves as a base class for both JSONArray and JSONObject. Documents are the main building blocks in the Document API. They can be saved, retrieved, updated etc. to/from Document using the Collections class.

Insert a Document: Insert()

Use the Collection.Insert() method to persist a Document object.

To insert a single document invoke the Insert() method:

Document doc = new JSONObject().put("Rank",1);
collectedDocs.Insert(doc);

To insert an array of elements as individual documents:

List<Document> topTwo = new ArrayList<>();
  topTwo.add(new JSONObject().put("Rank",1));
  topTwo.add(new JSONObject().put("Rank",2));
BulkResponse bulk = collectedDocs.Insert(topTwo);

System.out.println("Newly inserted document ids:");
for (String id : bulk.getIds())
    {
     System.out.print(id + " ");
    }
Retrieve a Document by id: Get()

To retrieve a document in a collection, invoke the Get() method and specify the document’s integer id:

Document doc = collectedDocs.Get("<id>");

where <id> is a quoted integer value (for example, Get("23").)

Delete a Document by id: Remove()

To delete a document from a collection, invoke the Remove() method and specify the document’s integer id:

collectedDocs.Remove("<id>");
Update a Document by id: Replace()

To update a document in a collection, invoke the Replace() method and specify the document’s integer id and the new data value:

collectedDocs.Replace("<id>",new JSONObject().put("Rank",4));
Insert or Update a Document by id: Upsert()

To either insert a document (if it does not exist) or update the document (if it does exist), invoke the Upsert() method and specify the document’s integer id and the new data value:

Document upsertDoc = new JSONObject().put("Rank",1);
String upsertId = collectedDocs.Upsert(upsertDoc);

Index and Query .NET Collections

Create or change an Index: CreateIndex()

To create an index to documents in a collection, invoke the CreateIndex() method and specify an index name and an index request object. The index request object must be JSON encoded. It can be simple or complex. To create a simple index, set the property “key” to the JSON path that you want to index. The default index type is bitmap. The following example creates an index:

collectedDocs.CreateIndex("indexName","{\"key\":\"HomeCity\"}");

If the specified index name already exists, CreateIndex() will redefine this index with the new index definition.

Delete an Index: DropIndex()

To delete an index to documents in a collection, invoke the DropIndex() method and specify an existing index name:

collectedDocs.DropIndex("indexName");

If the specified index name does not exist, DropIndex() performs no operation and does not return an error.

Query a Collection: CreateQuery()

To query an instance of a collection, invoke the CreateQuery() method that specifies the SELECT query. You then issue an execute() method that executes the query and creates a cursor that allows you to iterate through the query result set:

Query querySlams = collectedDocs.CreateQuery(
           "select Rank,\"First Name\",\"Last Name\",Country,Points,
            \"Grand Slams\" from JSON_TABLE(('ATPList'),
            ('{\"columns\":\"ATPList[*]\"}'))");
Cursor slamCursor = querySlams.execute();
System.out.println("Executed query on ATPList - results follow: ");
List<Document> slamList = slamCursor.GetAll();
slamList.forEach(doc -> System.out.println(doc.toJSONString()));
FeedbackOpens in a new tab