Manage a collection in Azure Cosmos DB for MongoDB using JavaScript

APPLIES TO: MongoDB

Manage your MongoDB collection stored in Azure Cosmos DB with the native MongoDB client driver.

Note

The example code snippets are available on GitHub as a JavaScript project.

API for MongoDB reference documentation | MongoDB Package (npm)

Name a collection

In Azure Cosmos DB, a collection is analogous to a table in a relational database. When you create a collection, the collection name forms a segment of the URI used to access the collection resource and any child docs.

Here are some quick rules when naming a collection:

  • Keep collection names between 3 and 63 characters long
  • Collection names can only contain lowercase letters, numbers, or the dash (-) character.
  • Container names must start with a lowercase letter or number.

Get collection instance

Use an instance of the Collection class to access the collection on the server.

The following code snippets assume you've already created your client connection and that you close your client connection after these code snippets.

Create a collection

To create a collection, insert a document into the collection.

// get database client for database 
// if database or collection doesn't exist, it is created
// when the doc is inserted

// insert doc
const doc = { name: `product-${random}` };
const insertOneResult = await client.db("adventureworks").collection("products").insertOne(doc);
console.log(`Insert 1 - ${JSON.stringify(insertOneResult)}`);

// insert docs
const docs = [
    { name: `product-${random}` },
    { name: `product-${random}` }
];
const insertManyResult = await client.db("adventureworks").collection("products").insertMany(docs);
console.log(`Insert many ${JSON.stringify(insertManyResult)}`);

Drop a collection

Drop the collection from the database to remove it permanently. However, the next insert or update operation that accesses the collection will create a new collection with that name.

// Drop the collection from the database, removing it permanently. 
// New accesses will create a new collection.

// drop from db instance
const dropCollection1 = await client.db("adventureworks").dropCollection("products");
console.log(`Collection dropped:\t${JSON.stringify(dropCollection1)}`);

// drop from collection instance
const dropCollection2 = await client.db("adventureworks").collection('products-2').drop();
console.log(`Collection dropped:\t${JSON.stringify(dropCollection2)}`);

The preceding code snippet displays the following example console output:

Collection dropped:     true
Collection dropped:     true
done

Get collection indexes

An index is used by the MongoDB query engine to improve performance to database queries.

// Get all indexes in collection
const collectionInstance = await client.db("adventureworks").collection('products')
const indexes = await collectionInstance.indexes();
console.log(`Indexes on collection:\n${Object.keys(indexes).map(key => `\t${key}: ${JSON.stringify(indexes[key])}\n`)}`);

The preceding code snippet displays the following example console output:

Indexes on collection:
        0: {"v":1,"key":{"_id":1},"name":"_id_","ns":"adventureworks.products"}
,       1: {"v":1,"key":{"name":1},"name":"name_1","ns":"adventureworks.products"}

done

See also