Creating Collections

You must create a collection in the MongoDB database before you can begin storing documents in it. To create a collection, you need to call createCollection(name, [options]) on the database handle. The name parameter is the name of the new collection. The optional options parameter is an object that can have the properties listed in Table 12.4, which define the behavior of the collection.

Image

Table 12.4 Options that can be specified when creating a collection

For example, the following line of code creates a new collection called newCollection in the testDB database, as shown in Figure 12.7:

db.createCollection("newCollection")

Image

Figure 12.7 Creating a new collection in the MongoDB shell.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset