MongoDB Basic Operation __mongodb

Source: Internet
Author: User
Tags mongodb collection mongodb server numeric value

This article records the basic operation of MongoDB, this is only the most basic, so it should be mastered.

Database

A database is a collection of physical containers. Each database has its own set of files on the file system. A single MongoDB server typically has multiple databases.

Collection

A collection is a set of MongoDB documents. It is equivalent to an RDBMS table. The collection exists in a single database. The collection does not perform a pattern. The documents within the collection can have different fields. Typically, all files in a collection are of the same or related purpose.

Document

A document is a set of key-value pairs. File dynamic mode. Dynamic mode means that documents in the same collection do not need a collection of common fields of the same field or structure group to hold different types of data.

A comparison between relational database and MongoDB

See this watch I think you must have your own understanding of MongoDB.

MongoDB supports a list of many data types given below:

String: This is the most commonly used data type to store data. The string in MongoDB must be a valid UTF-8.

Integer: This type is used to store a numeric value. The integer can be either 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 lowest and highest values of Bson elements.

Arrays: Stores a key with an array or list of this type or multiple values.

Timestamp: Time stamp. This can be handy when recording 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 used in languages reserved for a particular type of symbol.

Date: This data type is used to store the UNIX time format for the current date or time. You can specify your own date and time, date and year, month, day to create the object.

Object ID: This data type is used to store the ID of the document.

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

Let's start the real operation MongoDB

Show DBS: View the list of databases

Use command: This command will create a new database, and if it does not exist, it will return the existing database.

Use Mymongo (the created database Mymongo list does not exist.) To display the database, you need to insert it at least one file, such as: Db.test.insert ({name: "John", Age: "18"})

Dropdatabase () Method: the Db.dropdatabase () command is used to delete an existing database. It will delete the selected database. If no database has been selected, it will then delete the default ' test ' database

createcollection () method: Db.createcollection (name, options) is used to create a collection. Name is the names of the collections. Options is a file that specifies the set of configurations

Show Collections: You can view the list of created collections

In MongoDB, you do not need to create a collection. When you insert some files MongoDB a collection that is automatically created.

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

The drop () method returns True and returns False if the selected collection is discarded successfully

Insert () method: To insert data into the MongoDB collection, you need to use the MongoDB Insert () or Save () method.

Here are two kinds of query methods, described below. Here you can see me i inserted an array ["MongoDB", "Python", "Hadoop"]

Here MyCollection1 is the name of the collection. If the collection does not exist in the database, then MongoDB creates the collection and inserts it into the document. We can also insert the array, the same reason, you can try it yourself.

Insert the document, if we do not specify the _id parameter, and then mongodb this document to assign a unique objectid. _ID is a 12-byte hexadecimal number, each document in the unique collection.

Find () Method: Queries the collection data, the found () method will display all the files in an unstructured manner. The pretty () method displays the data in a formatted manner.

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

Below do the conditional query: RDBMS WHERE clause and MongoDB equivalent statement

and usage

or usage

The Limit () method and the Skip () method are commonly used as pagination: to limit Records in MongoDB, you need to use the Limit () method. The limit () method accepts a numeric parameter, which is the number of documents to display. The Skip () method also accepts parameters for numeric types and uses the number of skipped documents

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

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

Justone: (optional) If set to TRUE or 1, then only one file is deleted. If you have more than one record and only the first record to delete, set the Justone parameter in the Remove () method if you do not specify a delete condition, then MongoDB removes the entire file from the collection

Update () method: Update the document to update multiple sets of parameters to set ' multi ' to true

Sort () Method: Accepts a document that contains a list of fields along with their sort order. To specify sort order 1 and-1. 1 is used in ascending order, and-1 for descending. Note that if you do not specify sort precedence, then the sort () method displays the document in ascending order.

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.