Documentation Home
X DevAPI User Guide
Download this Manual

X DevAPI User Guide  /  Working with Documents

Pre-General Availability Draft: 2017-05-19

Chapter 5 Working with Documents

Once a collection has been created in the database, it can store JSON documents. Storing documents is as easy as passing a JSON data structure to the Collection.add() function. Some languages have direct support for JSON data, others have an equivalent syntax to represent that data. MySQL Connectors which implement X DevAPI aim to implement support for all methods that are native to the specific language.

In addition, the generic DbDoc objects can be used. The most convenient way to create them is by calling the Collection.newDoc(). DbDoc is a data type to represent JSON documents and how it is implemented is not defined. Languages implementing X DevAPI are free to follow an object oriented approach with getter and setter methods, or use a C struct style with public members.

For strictly typed languages it is possible to create class files based on the document structure definition of collections. MySQL Shell can be used to create those files.

Document Objects

Supported languages

Advantages

Native JSON

Scripting (JavaScript, Python)

Easy to use

JSON equivalent syntax

C# (Anonymous Types, ExpandoObject)

Easy to use

DbDoc

All languages

Unified across languages

Generated Doc Classes

Strictly typed languages (C#)

Natural to use

The following example shows the different methods of inserting documents into a collection.

MySQL Shell JavaScript Code

// Create a new collection 'my_collection'
var myColl = db.createCollection('my_collection');

// Insert JSON data directly
myColl.add({name: 'Sakila', age: 15});

// Inserting several docs at once
myColl.add([ {name: 'Susanne', age: 24},
  {name: 'Mike', age: 39} ]);

MySQL Shell Python Code

# Create a new collection 'my_collection'
myColl = db.create_collection('my_collection')

# Insert JSON data directly
myColl.add({'name': 'Sakila', 'age': 15})

# Inserting several docs at once
myColl.add([ {'name': 'Susanne', 'age': 24},
  {'name': 'Mike', 'age': 39} ])

Node.js JavaScript Code

// Create a new collection 'my_collection'
db.createCollection('my_collection').then(function(myColl) {

  // Insert JSON data directly
  myColl.add({name: 'Sakila', age: 15});

  // Inserting several docs at once
  myColl.add([ {name: 'Susanne', age: 24},
    {name: 'Mike', age: 39} ]);

  // Insert Documents
  var myDoc = {};
  myDoc.name = 'James';
  myDoc.age = 47;
  myColl.add(myDoc);
})

C# Code

{
  // Create a new collection "my_collection"
  var myColl = db.CreateCollection("my_collection");

  // Insert JSON data directly
  myColl.Add(new { name = "Sakila", age = 15 }).Execute();

  // Inserting several docs at once
  myColl.Add(new[] {new { name = "Susanne", age = 24},
	new { name = "Mike", age = 39} }).Execute();

  // Insert Documents
  var myDoc = new DbDoc();
  myDoc.SetValue("name", "James");
  myDoc.SetValue("age", 47);
  myColl.Add(myDoc).Execute();

  //Fetch all docs
  var docResult = myColl.Find().Execute();
  var docs = docResult.FetchAll();


}

Java Code

// Create a new collection 'my_collection'
Collection coll = db.createCollection("my_collection");

// Insert JSON data directly
coll.add("{\"name\":\"Sakila\", \"age\":15}");

// Insert several documents at once
coll.add("{\"name\":\"Susanne\", \"age\":24}",
        "{\"name\":\"Mike\", \"age\":39}");

// Insert Documents
DbDoc myDoc = new DbDoc();
myDoc.add("name", new JsonString().setValue("James"));
myDoc.add("age", new JsonNumber().setValue("47"));
coll.add(myDoc); 

C++ Code

// Create a new collection 'my_collection'
Collection myColl = db.createCollection("my_collection");

// Insert JSON data directly
myColl.add(R"({"name": "Sakila", "age": 15})").execute();

// Inserting several docs at once
std::list<DbDoc> docs = {
  R"({"name": "Susanne", "age": 24})",
  R"({"name": "Mike", "age": 39})"
};
myColl.add(docs).execute();

User Comments
Sign Up Login You must be logged in to post a comment.