.createCollection()

Creates a new collection where data will be stored in a separate file.

createCollection(name, defaultValues)

Parameter

Default

Description

name

String

The name for the collection

defaultValues

{}

Data (optional)

Default values for omitted keys

Returns

Description

The new collection

const Users = db.createCollection('users');

With default values:

const People = db.createCollection('people', {
  name: 'Unnamed person',
  hobbies: []
});

With incrementable properties:

Incrementable properties were added in the version 2.6.0.

They are used to assign an automatic value to an entry from the collection, incrementing its value when a new entry is inserted into the collection.

To represent an incrementable property, you must put a dollar sign ($) before the property name and provide its initial value. The dollar sign will not appear in the entry's properties (ex.: $id will set a property named id).

Incrementable properties only work with values of type Number.

The Typescript typings for incrementable properties are not yet properly working as of version 2.13.0.

const People = db.createCollection('people', {
  $id: 0, // Incrementable property, starting with the value 0
  name: 'Unnamed person',
  hobbies: []
})

Last updated