Getting Started with MongoDB

Source: Internet
Author: User

First, MongoDB

MongoDB is an open source, free, non-relational database (NOSQL). It has a fast and easy to use, data format and JSON correspondence, and so on, the most important feature is that all data durable operation without the developer to manually write SQL statements, directly invoke the method can easily implement the operation of the database. MongoDB is: http://www.mongodb.org/downloads.

Install the following:

Select the appropriate version on the download page, and the version used here is Windows version mongodb-win32-i386-2.4.12. Unzip the compressed file into the C:\MONGOBD directory. and create the data/db directory in the C-drive directory (the path for storing the data files in Windows Class MongoDB).

Start:

CMD into the C:\mongodb\bin directory, execute the command mongod.exe,mongodb the database to start.

Connect to MongoDB

CMD into the C:\mongodb\bin directory, execute the command mongo.exe, you can connect the MongoDB database.

Second, MONGODB data structure

MongoDB does not exist tables, and the concepts of records are somewhat different from the usual relational databases. MongoDB has several concepts such as:

Database: Similar to a relational database, you can create multiple databases in MongoDB.

Collection: A table that can be likened to a relational database. Only the record fields in the table are the same, and the structure of the documents stored in the collection can be different. For example, the structure of two documents is inconsistent and can still be stored. The collection store in the database.

Document: A field that can be likened to a table in a relational database. The document is primarily stored in the collection. Multiple keys (in the form of a Key-value value) and their associated values are placed together in an orderly manner as documents. Bson stores data in MongoDB using a class JSON.

The data types supported by MongoDB are as follows:

    • String: String type, strings in MongoDB must be valid UTF-8.
    • Integer: the numeric type. Integers can be either 32-bit or 64-bit.
    • Boolean: A Boolean value (True/false).
    • Double: Floating-point type.
    • Min/max keys: This type is used to compare the lowest and highest values of the Bson element.
    • Arrays: An array or list.
    • Timestamp: Timestamp. Can log files that have been modified or added.
    • Object: Used for embedded files.
    • NULL: Stores a null value.
    • Symbol: The same as a string, but it is usually used in a language reserved for a particular symbol type.
    • Date: The UNIX time format used to store the current date or time.
    • Object ID: The ID of the document.
    • Binary data: this binary.
    • Code: The JavaScript code that is used to store the document.
    • Regular expression: Used to store regular expressions

Third, related operations

1. Database

Show DBS  --  View all databases  use test  --  creates a database that does not exist in the test list. To display the database, you need to insert it into at least one file. Execute the following command: Db.movie.insert ({"Name": "Tutorials Point"})--  Delete database

2. Collection

-- View all collections under this data  -  -Create person collection  6142800Max10000--  Creates a MyCol collection and sets the related property Db.person. Drop -- Delete Collection

3. Documentation

A, add: Call MongoDB's Insert () or Save () method.

Db.person.Insert({name: "Lisi", Age: -, Gender: "FEMAIL", Tags:[' MongoDB ', ' database ', ' NoSQL ']});--to add a collection in the person collectionDb.person.Insert([{name: "Lisi2", age:21, Gender: "FEMAIL", Tags: [' database ', ' NoSQL ']},{name: "Lisi2", Age: A, Gender: "Mail", Tags:[' MongoDB ', ' database ']}]);--to add a collection in the person collectionDb.person.Save({name: "Lisi4", Age: +, Gender: "Mail", Tags:[' MongoDB ', ' NoSQL ']});--to add a collection in the person collection

When you add a document using Insert (), Save (), the collection is automatically created if the collection does not exist.

B, modify: The update () method can be called, the method is defined as follows:

Db. Collection_name.update (criteria, objnew, Upsert, Multi)
The criteria:update query condition, similar to that in SQL Update query, is behind.
Objnew: The object to update and some updated operators, such as $, $inc ... ) can also be understood as the SQL update query within the set after the
Upsert: This parameter means that if there is no record of update, insert Objnew,true is inserted, default is False, not inserted.
Multi:mongodb default is False, only update the first record found, if this parameter is true, the condition is checked out all the records are updated.

Db.person. Update ({name: "Zhangsan3"}, {$set--   Modify

C, query: Use the Find () method

--db.person.find (); -- Query All db.person.find ({age:--  query for document ages 20 db.person.find -  and use, query for document Db.person.find with age 20 and name Zhangsan ({$or:[{"Name": " Zhangsan2 "}, {age:20}]--  or use, query aname for zhangsan2 or GE 20 documents

Other related query condition syntax is as follows:

Less than: syntax {<key>:{$lt: <value>}}, such as Db.person.find ({"Age": {$lt: 50}})
Less than or equal to: {<key>:{$lte: <value>}}, such as Db.person.find ({"Age": {$lte: 50}})
Greater than: {<key>:{$gt: <value>}}, such as Db.person.find ({"Age": {$gt: 50}})
Greater than equals: {<key>:{$gte: <value>}}db.person.find ({"Age": {$gte: 50}})
Not equal to: {<key>:{$ne: <value>}} db.person.find ({"Age": {$ne: 50}})

D, Delete: Use the Remove () method

Db.person.remove ({age:---  Delete the document aged 20 Db.person.remove ({ages:1 }, -- Delete a document of age 20 first, 1,justone parameter -- Delete all

Iv. Other Orders

1. Query filtering

1 indicates that the display name,0 indicates that the _id is not displayed.

Db.person.find ({},{"name": 1,_id:0})

2. Limit

Db.person.find (). Limit (2--  Show only two documents

3. Skip

Db.person.find (). Limit (1). Skip (1);  -- only the second document is displayed. 

4. Sort

Db.person.find (). Sort ({age:1--  documents are sorted by age, ascending

Sort order 1 Ascending, -1 reverse.

Getting Started with MongoDB

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.