Novice MongoDB Learning---(ii) MongoDB database, Object, collection

Source: Internet
Author: User
Tags object object reserved

Multiple databases can be created in a mongodb.

MongoDB's default database is "db", which is stored in the data directory.

Databases can be created in MongoDB. If you want to use MongoDB, creating a database is not necessary.

The "show dbs" command displays a list of all data.

[email protected]: ~ $ mongo
MongoDB shell version: 2.0.4
connecting to: test
> show dbs
NewsDB 0.203125GB
local (empty)

Execute the "db" command to display the current database object or collection.
[email protected]: ~ $ mongo
MongoDB shell version: 2.0.4
connecting to: test
> db

Run the "use" command to connect to a specified database.
[email protected]: ~ $ mongo
MongoDB shell version: 2.0.4
connecting to: test
> db
> Use NewsDB
switched to db NewsDB
> db

The database name can be any character, but it cannot contain empty strings, periods (.), Or "".

"system" as a system reserved string cannot be used as a database name.

The database name cannot contain "$".

Document is the most core concept in mongodb, and its core unit. We can compare documents to each row of data in a relational database.

Multiple keys and their associated values are placed together in order to form a document. Store data in mongodb using a json-like bson.

Bson data can be understood as adding some data types that are not in json on the basis of json.

If we can json, then we have half of the bson. As for the newly added data types, I will introduce them later.

An example of the document is shown. This is the information I scraped from Netease News using a crawler and stored it in the database.

"_id": ObjectId ("557a46d86e530e0e770001e4"),
"from_url": "",
"news_body": [
"June 3rd-Beijing, during the 7th Cloud Computing Conference, Beijing Cloud Times' 2015 Desktop Cloud Ecological Chain Conference entitled" New Desktop New Ecology New Value "was successfully held at the National Convention Center.",
"At the conference site, Mr. Jiang Jianping, general manager of Cloud Times, analyzed the common problems and challenges faced by the current desktop cloud industry with actual cases, and how to classify and standardize application scenarios, standardize the combination of scenario requirements, and standardize installation and deployment. 2. Standardization of delivery tools, together with ecological chain partners with "Desktop Cloud" and "Industry 4.0" ideas to jointly solve common problems in the industry and enhance corporate IT value. ",
"At the meeting, Jiang Jianping, general manager of Cloud Times, proposed to build a desktop cloud ecological chain to achieve application scenario standardization, ecological integration (product certification and optimization standardization), product form integration standardization, deployment integration standardization, equipment online standardization, resource expansion standardization, etc. The formulation not only refreshed and inspired the audience, but was also unanimously agreed by the partners of the ecological chain. ",
"As the core strategic partners of the desktop cloud ecological chain in the cloud era, representatives of Zhongke Shuguang, 360, H3C, Aishu Software, and Jida Zhengyuan all sent important speeches and participated in the discussion of the round table forum. From left to right: Shuguang Information He Mujun, general manager of the cloud computing product division, Zhang Xiaobing, deputy general manager of the 360 cloud security division, Wang Donghai, director of the H3C marketing department and solution department, Zhu Mingda, deputy general manager of Jida Zhengyuan, and Wu Mixiang, vice president of Aishu software products
"In addition, many IT vendors from the desktop cloud ecosystem in the cloud era also conducted lively discussions on hot topics such as" software localization "," industrial internet ", and" industry 4.0 ".",
"There were 20 product experience desks at the conference site, which focused on the unified management portal products integrated with ecological chain manufacturers' products and a full range of cloud software and hardware integrated products in the cloud era. Participants can operate and experience the desktop cloud products of the cloud era in person. Main functions and performance modules. During the product demonstration process and user experience, desktop cloud products in the cloud era are distinguished by their excellent graphic design, high-definition video, optimized storage, and complex peripheral support. Leadership has been affirmed by users. "
"news_from": "Netease Technology Report",
"news_thread": "AR99BQAF00094P25",
"news_time": "2015-06-04 14:43:47",
"news_title": "2015 Desktop Cloud Ecological Chain Conference Successfully Held in the Cloud Era",
"news_url": ""

Generally, the "object" term refers to a file.

A file is similar to an RDBMS record.

We can perform insert, update, and delete operations on collections.

The following table will help you understand some concepts in Mongo more easily:

Table Collection
Column Key
Value Value
Records / Rows Document / Object
The following table shows several data types commonly used in MongoDB.

Data type Description
string can be an empty string or a combination of characters.
integer An integer.
boolean Logical value True or False.
double double precision floating point
null is not 0 or empty.
array: A series of values
object Object, the entity used in the program. Can be a value, variable, function, or data structure.
timestamp timestamp is stored as a value of 64, which can be guaranteed to be unique when running only one mongod. The first 32 bits hold the UTC time in seconds. The last 32 bits are the count value in this second. Starting from 0, each new MongoTimestamp object is incremented by one.
Internationalized Strings UTF-8 strings.
Object IDs in mongodb documents need to use the unique keyword _id to identify them. Almost every mongodb document uses the _id field as the first attribute (with some exceptions in system collections and capped collections). The _id value can be of any type. The most common method is to use the ObjectId type.

A collection is a collection of documents. If a document is likened to a row in a database, a collection can be likened to a database table.

Collections in mongodb are schemaless, which means that the structure of the documents stored in the collection can be different. For example, the following two documents can be stored in a collection at the same time:

 {"name": "jingdong"} {"Name": "jingdong", "sex": "man"}
Legal collection name
The collection name must begin with a letter or underscore.

Collection names protect numbers

Collection names cannot have dollar signs "$", "$" is a system reserved character.

The name of the collection cannot exceed 128 characters.

In addition, the use of "." Numbers is allowed in the collection, they are called Subcollection; for example, if you have a blog collection, you can use blog.title, blog.content or to help you group more Well organized collection.

The following examples:

 db.tutorials.php.findOne ()

capped collections
Capped collections are fixed-size collections. A bit of a circular queue.

It has high performance and queue expiration (expiration in the order of insertion). It is a bit similar to the "RRD" concept.

Capped collections are high-performance automatic maintenance of the insertion order of objects. It is very suitable for logging-like functions. Unlike a standard collection, you must explicitly create a capped collection, specifying the size of a collection in bytes. The data storage space of the collection is allocated in advance.

It should be noted that the specified storage size contains the database header information.
db.createCollection ("mycoll", {capped: true, size: 100000})
In the capped collection, you can add new objects.
Can be updated, however, objects do not increase storage space. If it increases, the update will fail.
The database does not allow deletion. Use the drop () method to delete all rows in the collection.
Note: After deleting, you must explicitly recreate the collection.
In 32bit machines, the maximum size of the capped collection is 1e9 (1X109) bytes.
Novice mongoDB learning-(2) MongoDB database, objects, collections

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: 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.