Databases and collections (MongoDB document translation and interpretation)

Source: Internet
Author: User
Tags mongo shell

MongoDB stores BSON files as data records into the collection; The following is the collection in the database.

First, Databases

In MongoDB, the database saves a document-style collection.
In the MONGO shell, if you want to select a certain database to use, you can use the using command, as in the following example:

Use MyDB

1.1 Creating a database Create a databases

If the database does not exist, MongoDB will create this database the first time you store data for this database. Therefore, you can switch to a nonexistent database and execute the following command in the MONGO Shell:

Use mynewdb
Db.myNewCollection1.insertOne ({x:1})

where the Insertone () operation creates both the mynewdb database and the MyNewCollection1 collection Even if they do not exist at first.
For a list of database name restrictions, name restriction.

Second, set collections

MongoDB stores the document into the collection. Collections are similar to tables in relational databases.

2.1 Creating a collection Create a Collection

If the collection does not exist at the beginning, MongoDB will create this collection when you first store the data to this collection.

Db.myNewCollection2.insertOne ({x:1})
Db.myNewCollection3.createIndex ({y:1})

For a list of restricted names for the collection, click View [Name Restrictions] (
https://docs.mongodb.com/manual/reference/limits/#restrictions-on-collection-names).

2.2 Creating an explicit

MongoDB provides db. The CreateCollection () method explicitly creates a collection with various option settings, such as setting a maximum size or a document validation rule. If you do not specify these options, you do not need to explicitly create a collection because MongoDB creates a new collection when you first store the data for collecitons.
If you want to modify the option settings for these collections, view the Collmod collection template.

2.3 Document Validation

New in version 3.2.
By default, a collection does not require a document to have the same schema, that is, a document in a collection does not need to have the same set of fields, and the data type of each field can be different in one collection.
Starting with MongoDB 3.2, you can apply document validation rules when you make updates or insert data into a collection. See document validation for more details.

2.4 Modifying the document schema

You can modify the schema of a document in a collection, such as adding new fields, removing old fields, modifying the data type of a field, and eventually updating the document into a new schema.



Null

Databases and collections (MongoDB document translation and interpretation)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.