MongoDB basic operation learning notes

Source: Internet
Author: User
Tags install mongodb mongodb server

MongoDB basic operation learning notes

Record the basic operations of MongoDB today. This is only the most basic operation, so it should be mastered.

Database

A database is a collection of physical containers. Each database has its own file system. A single MongoDB server usually has multiple databases.

Set

A collection is a set of MongoDB documents. It is equivalent to an RDBMS table. Collection exists in a single database. Set non-execution mode. Documents in the set can have different fields. Generally, all files in a collection are for the same or related purpose.

Document

A document is a set of key-value pairs. File dynamic mode. Dynamic Mode means that a document in the same set does not need a set of public fields of the same field or structure group, and can accommodate different types of data.

Comparison between RDS and MongoDB

When you see this table, I think you must have your own understanding of MongoDB.

The list of many data types supported by MongoDB is shown below:

  • String: This is the most common data type to store data. Strings in MongoDB must be valid UTF-8.
  • Integer: This type is used to store a value. The integer can be 32-bit or 64-bit, depending on your server.
  • Boolean: This type is used to store a Boolean value (true/false ).
  • Double: This type is used to store floating point values.
  • Min/Max keys: This type is used to compare the minimum and maximum values of BSON elements.
  • Arrays: use this type of array or list or multiple values to store them in one key.
  • Timestamp: Timestamp. This allows you to easily record files that have been modified or added.
  • Object: This data type is used for embedded files.
  • Null: This type is used to store a Null value.
  • Symbol: This data type is used for the same string, but it is usually reserved for the language of the specific Symbol type.
  • Date: This data type is used to store the UNIX time format of the current Date or time. You can specify your own date and time, date and year, month, and day to create an object.
  • Object ID: the ID of the data type used to store documents.
  • Binary data: This data type is used to store Binary data.
  • Code: This data type is used to store JavaScript Code in the document.
  • Regular expression: This data type is used to store Regular expressions.

The following describes how to operate MongoDB.

Show dbs: view the Database List

Use command: This command creates a new database. If it does not exist, the existing database is returned.

Use myMongo (the myMongo list of the created database does not exist. To display the database, you need to insert at least one file, such as db. test. insert ({name: "Michael", age: "18 "}))

DropDatabase () method: the db. dropDatabase () command is used to delete an existing database. It will delete the selected database. If no database is selected, the default 'test' database is deleted.

CreateCollection () method: db. createCollection (name, options) is used to create a set. name is the name of the set. Options is a file used to specify the set of configurations.

Show collections: You can view the list of created collections.

In MongoDB, you do not need to create a set. A set automatically created by MongoDB when some files are inserted.

Drop () method: db. collection. drop () is used to delete a set from the database.

The drop () method returns true. If the collection is successful, it is discarded. Otherwise, false is returned.

Insert () method: To insert data to the MongoDB set, you must use the insert () or save () method of MongoDB.

Two query methods are used here, which will be described below. Here you can see that I inserted an array ["MongoDB", "python", "Hadoop"]

Here myCollection1 is the name of the set. If the set does not exist in the Database, MongoDB creates the set and inserts it into the document. We can also insert arrays. The truth is the same. You can try it on your own.

Insert this document. If we do not specify the _ id parameter, MongoDB will assign a unique ObjectId to this document. _ Id is the hexadecimal number of 12 bytes. It is the only document in a set.

The find () method is used to query collection data. The find () method displays all files in an unstructured manner. The pretty () method displays the data in formatted format.

The above example has already been shown. In addition to the find () method, there is a findOne () method that returns a file.

Perform the following conditional query: The RDBMS Where clause is equivalent to the MongoDB clause.

AND usage

OR usage

The Limit () method and the skip () method are generally used as pages: to limit records in MongoDB, the Limit () method is required. The limit () method accepts a numeric parameter, which is the number of documents to be displayed. The skip () method also accepts numeric parameters and uses the number of skipped documents

Remove (): Used to delete a document from a collection. The remove () method accepts two parameters. The first is to delete criteria, and the second is the justOne flag:

Deletion criteria: (optional) delete standard, which will be deleted according to the file.

JustOne: (optional) If set to true or 1, delete only one file. If there are multiple records and only the first record is to be deleted, set the justOne parameter in the remove () method.

If the deletion condition is not specified, MongoDB deletes the entire file from the collection.

Update () method: Update the document. To Update multiple objects, set 'multi 'to true.

Sort () method: accept a document containing a list of fields and their sorting order. Specify the Order 1 and-1. 1 is used for ascending order, and-1 is used for descending order. Note that if no sorting priority is specified, the sort () method then displays the document in ascending order.

Let's write it here. I have time to write some in-depth information. I hope you can advise me.

For more information about MongoDB, see the following links:

MongoDB 3.0 official version released and downloaded

CentOS compilation and installation of MongoDB

CentOS compilation and installation of php extensions for MongoDB and mongoDB

CentOS 6 install MongoDB and server configuration using yum

Install MongoDB2.4.3 in Ubuntu 13.04

MongoDB beginners must read (both concepts and practices)

MongoDB Installation Guide for Ubunu 14.04

MongoDB authoritative Guide (The Definitive Guide) in English [PDF]

Nagios monitoring MongoDB sharded cluster service practice

Build MongoDB Service Based on CentOS 6.5 Operating System

MongoDB details: click here
MongoDB: click here

This article permanently updates the link address:

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.