Skip to the content.

Manipulating the collection

These functions implement the HTTP API for modifying collections.

collection.create

async collection.create([properties]): Object

Creates a collection with the given properties for this collection’s name, then returns the server response.

Arguments

Examples

const db = new Database();
const collection = db.collection('potatoes');
await collection.create()
// the document collection "potatoes" now exists

// -- or --

const collection = db.edgeCollection('friends');
await collection.create({
  waitForSync: true // always sync document changes to disk
});
// the edge collection "friends" now exists

collection.load

async collection.load([count]): Object

Tells the server to load the collection into memory.

Arguments

Examples

const db = new Database();
const collection = db.collection('some-collection');
await collection.load(false)
// the collection has now been loaded into memory

collection.unload

async collection.unload(): Object

Tells the server to remove the collection from memory.

Examples

const db = new Database();
const collection = db.collection('some-collection');
await collection.unload()
// the collection has now been unloaded from memory

collection.setProperties

async collection.setProperties(properties): Object

Replaces the properties of the collection.

Arguments

Examples

const db = new Database();
const collection = db.collection('some-collection');
const result = await collection.setProperties({waitForSync: true})
assert.equal(result.waitForSync, true);
// the collection will now wait for data being written to disk
// whenever a document is changed

collection.rename

async collection.rename(name): Object

Renames the collection. The Collection instance will automatically update its name when the rename succeeds.

Examples

const db = new Database();
const collection = db.collection('some-collection');
const result = await collection.rename('new-collection-name')
assert.equal(result.name, 'new-collection-name');
assert.equal(collection.name, result.name);
// result contains additional information about the collection

collection.rotate

async collection.rotate(): Object

Rotates the journal of the collection.

Examples

const db = new Database();
const collection = db.collection('some-collection');
const data = await collection.rotate();
// data.result will be true if rotation succeeded

collection.truncate

async collection.truncate(): Object

Deletes all documents in the collection in the database.

Examples

const db = new Database();
const collection = db.collection('some-collection');
await collection.truncate();
// the collection "some-collection" is now empty

collection.drop

async collection.drop([properties]): Object

Deletes the collection from the database.

Arguments

Examples

const db = new Database();
const collection = db.collection('some-collection');
await collection.drop();
// the collection "some-collection" no longer exists