# Initialization

We need to get a reference to the database before we can use the database API to perform create, delete, modify, and query operations. The following call is made to obtain a reference to the database of the default environment:

const db = wx.cloud.database()

To get a reference to the database of another environment, pass in an object parameter at the time of the call, where the environment to be used is specified by the env field. In this case, the method returns a reference to the test environment database.

Example: Suppose we have an environment called test used as a test environment, we can get the following test environment database:

const testDB = wx.cloud.database({
  env: 'test'
})

To work with a collection, get its reference first. After obtaining the database reference, we can obtain the reference of a collection through the collection method on the database reference. For example, to get the to-do list collection:

const todos = db.collection('todos')

Getting a reference to a collection does not initiate a network request to pull its data. We can use this reference to perform create, delete, modify, and query operations. In addition, we can get a reference to a record with a specified ID in the collection through the doc method of the collection. Similarly, a reference to a record can be used to update or delete a particular record.

Suppose we have a todo with the ID todo-identifiant-aleatoire. In this case, we can use the doc method to obtain its reference:

const todo = db.collection('todos').doc('todo-identifiant-aleatoire')

In the next chapter, we will learn to insert data into the collection.