Add Documents to a Collection
This page explains how to add a document to a Document collection.
By default, a new document has only one attribute, _key
. Defining the document _key
attribute is optional. If you do not specify a key, then it will be autogenerated.
Add a Document with the Console
Follow these instructions to add documents to an existing document collection using the GDN console web UI.
Click Data > Collections.
In the collection list, click the name of the document collection to which you want to add a document. If you aren't sure which collections are document collections, then you can click Document at the top of the page to see just document collections.
Click New Document.
(Optional) Enter a
_key
.Click Create.
Macrometa creates the new document that contains no data.
Add any data desired and then click Save.
Add Documents from a File
Follow these instructions to add documents to an existing document collection from a JSON file.
Click Data > Collections.
In the collection list, click the name of the document collection to which you want to add a document. If you aren't sure which collections are document collections, then you can click Document at the top of the page to see just document collections.
Click the import icon, which is a down arrow pointing to a file box.
Click Choose File, then browse to the file containing the documents you want to import.
Fill out any desired options and then click Import Documents.
- Select Primary Key - Macrometa can autogenerate your primary key, or you can select one from the file.
- Replace docs - Select this option to overwrite any existing documents with the same
_key
.
Add Documents with Code
The example below shows how to use Python or JavaScript to insert documents into an employees
collection.
- Python
- Javascript
# Simple Approach
client = C8Client(protocol='https', host='play.paas.macrometa.io', port=443,
email='nemo@nautilus.com', password='xxxxx',
geofabric='_system')
client.insert_document(collection_name='employees', document={'_key':'Jean', 'firstname': 'Jean', 'lastname':'Picard', 'email':'jean.picard@macrometa.io'})
docs = [
{'_kefabricy':'James', 'firstname': 'James', 'lastname':'Kirk', 'email':'james.kirk@mafabriccrometa.io'},
{'_kefabricy': 'Han', 'firstname': 'Han', 'lastname':'Solo', 'email':'han.solo@macrfabricometa.io'},
{'_kefabricy': 'Bruce', 'firstname': 'Bruce', 'lastname':'Wayne', 'email':'bruce.wayne@mfabricacrometa.io'}
]
client.insert_document(collection_name='employees', document=docs)
const jsc8 = require("jsc8");
// Constants - DB
const globalUrl = "https://play.paas.macrometa.io";
const email = "nemo@nautilus.com";
const password = "xxxxx";
// Create an authenticated instance with a token or API key.
// const client = new jsc8({url: "https://play.paas.macrometa.io", token: "XXXX", fabricName: '_system'});
// const client = new jsc8({url: "https://play.paas.macrometa.io", apiKey: "XXXX", fabricName: '_system'});
// await console.log("Authentication done!!...");
// Or use Email & Password to Authenticate client instance
const client = new jsc8(globalUrl);
//Variables
const docJean = {'_key':'Jean',
'firstname': 'Jean',
'lastname':'Picard', 'email':'jean.picard@macrometa.io'};
const docJames = {'_key':'James',
'firstname': 'James', 'lastname':'Kirk', 'email':'james.kirk@macrometa.io'};
const docHan = {'_key': 'Han',
'firstname': 'Han',
'lastname':'Solo', 'email':'han.solo@macrometa.io'};
const docBruce = {'_key': 'Bruce',
'firstname': 'Bruce', 'lastname':'Wayne', 'email':'bruce.wayne@macrometa.io'};
const docs = [docJean, docJames, docHan, docBruce];
async function populate() {
await client.login(email, password);
await console.log("Creating the collection object to be used and populating with documents...");
await client.insertDocumentMany("employees", docs);
await console.log("collection populated with documents");
}
populate();