MONGO database.

Source: Internet
Author: User
Tags install mongodb mongo shell

The production environment uses the MONGO database.

Long-coveted for the MONGO database, its high performance/open source/modeless/document-based database/... What a tempting feature, also occasionally tossing and playing, but did not make notes, take advantage of this opportunity to pick up again.

About MONGO

MongoDB is a Open-source document database that provides high performance, high availability, and automatic scaling.

MONGO supported query language is very powerful, its syntax is somewhat similar to object-oriented query language, almost can achieve similar relational database single table query most functions

MONGO is a NoSQL database

NOSQL, the full name is not-only Sql, refers to the non-relational database. It mainly solves several key points: non-relational, distributed, open-source, horizontally extensible. The original purpose was for large-scale Web applications. Typical features such as freedom of mode, support for simple replication, simple APIs, eventual consistency (non-acid), high-capacity data, etc. NoSQL is stored as many key-value as we use the most.

MONGO characteristics
    • Mode of freedom.
    • Supports dynamic queries.
    • Supports full indexes, including internal objects.
    • Support Queries.
    • Supports replication and recovery.
    • The file storage format is Bson (an extension of JSON).
    • can be accessed over the network.
    • Support for Python,php,ruby,java,c,c#,javascript,perl and C + + language drivers
    • Use efficient binary data storage, including large objects such as video.
Software as a service

Use MONGO as a service to crud our data through an interface.

For different user roles, the understanding required for MONGO is different, and our role is developers, which we use primarily as a service.

Details can be consulted reference

And as a Python developer, we use the MONGO Python driver to crud the data

Installation

Ubuntu installation MONGO is relatively simple, directly sudo apt-get install mongodb on the line.

Of course, can also go to the official website to download installation

Port number is27017

Enter the MONGO Shell:mongo

Basic 6 Simple concepts
    • MongoDB has the same concept as the ' database ' you know well. Each database is a high-level container for storing all other data.

    • A database can have several ' collections ' (collection). The ' table ' in collections and traditional concepts has a lot in common.

    • A collection consists of several ' documents ' that can be thought of as ' rows '.

    • The document is also made up of several ' fields ', and the field is like ' column '.

    • 索引The meaning of (index) in MongoDB is just like indexing in an RDBMS.

    • 游标(cursor) is different from the above 5 concepts. Whenever data is requested from MongoDB, it always returns a cursor. Based on cursors we can do things like counting or skipping directly, without actually reading the data.

A summary, MongoDB consists of the composition, the 数据库 database consists 集合 of a set composed of 文档 . The composition of the document, the collection can be 索引 , thereby improving the performance of finding and sorting. Finally, when we read the data from MongoDB, the 游标 cursor does not actually read the operation unless needed.

Relational databases are defined at this level , and a document-oriented database is defined at 文档 this level . In other words, each document in the collection can have a separate domain. Therefore, although a collection is a simplified container relative to a table, the document contains much more information than the line.

MONGO Shell Entry

help。 The shell returns the globally available operations. Like whatshow dbs

The MONGO shell runs on top of JavaScript.
You can perform a number of global directives, such as Help or exit.
Action Object DB to perform operations against the current database, such as db.help () or db.stats ().

    • First we use the Global command to switch the database. Input use learn . The existence of this database is not related, and if not, new. Z You're already in a database after execution.
    • db.unicorns.insert({name: ‘Aurora‘, gender: ‘f‘, weight: 450})
      The above command performs an insert operation on the Unicorns object and passes in a parameter. Inside MongoDB, the data is stored in a binary serial JSON format.
    • If we now execute db.getcollectionnames (), we will see two collections: Unicorns and system.indexes. System.indexes is created in each database and contains the index information in the database.
    • You can now execute the find command on the Unicorns object, listing all the documents:db.unicorns.find()
      Note that in addition to the various fields that you enter in the document, there is a domain called _id.
    • Insert a completely different document toward unicorns. db.unicorns.insert({name: ‘Leto‘, gender: ‘m‘, home: ‘Arrakeen‘, worm: false})use Find again to list all the documents
Mastering Selectors (selector)

MongoDB also has a very useful concept: Query selector (selector).
MongoDB's query selector is like a where statement in SQL code.
So you can use it to find, count, update, or delete documents in the collection.
The selector is a JSON object, and the simplest form is {}, which is used to match all documents.
If we need to find all the females ' unicorns (unicorn), we can match them with the selector {gender: ' F '}.

    • By db.unicorns.remove Deleting all the data we entered in the Unicorns collection before
    • Insert the following data:
Db.unicorns.insert ({name: ' horny ', dob:new Date (1992,2,13,7,47), loves: [' carrot ', ' papaya '], weight:600, Gender: ' m ', V AMPIRES:63});d B.unicorns.insert ({name: ' Aurora ', Dob:new Date (1991, 0, 0,), loves: [' carrot ', ' grape '], weight:4  Gender: ' F ', vampires:43});d B.unicorns.insert ({name: ' Unicrom ', Dob:new Date (1973, 1, 9, A, ten), Loves: [' Energon ',  ' Redbull '], weight:984, Gender: ' m ', vampires:182});d B.unicorns.insert ({name: ' Roooooodles ', Dob:new Date (1979, 7, 18, Loves: [' apple '], weight:575, Gender: ' m ', vampires:99});d B.unicorns.insert ({name: ' Solnara ', dob:new Date (19 6, 4, 2, 1), loves:[' apple ', ' carrot ', ' chocolate '], weight:550, Gender: ' F ', vampires:80});d B.unicorns.insert ({name: ' Ayna ', Dob:new Date (1998, 2, 7, 8,), Loves: [' strawberry ', ' lemon '], weight:733, Gender: ' F ', vampires:40});d B.unic Orns.insert ({name: ' Kenny ', Dob:new Date (1997, 6, 1, ten), Loves: [' grape ', ' lemon '], weight:690, Gender: ' m ', Vampir es:39});d B.unicorns.insert({name: ' Raleigh ', Dob:new Date (2005, 4, 3, 0,), loves: [' apple ', ' sugar '], weight:421, Gender: ' m ', vampires:2});d b . Unicorns.insert ({name: ' Leia ', Dob:new Date (2001, 9, 8, +), loves: [' apple ', ' watermelon '], weight:601, gender: ' F ', vampires:33});d B.unicorns.insert ({name: ' Pilot ', Dob:new Date (1997, 2, 1, 5, 3), loves: [' apple ', ' watermelon '], Weig ht:650, Gender: ' m ', vampires:54});d B.unicorns.insert ({name: ' Nimue ', Dob:new Date (1999, one, A,,), loves: [' grap E ', ' carrot '], weight:540, Gender: ' F '});d B.unicorns.insert ({name: ' Dunx ', Dob:new Date (1976, 6,,), Loves: [' g Rape ', ' watermelon '], weight:704, Gender: ' m ', vampires:165});

{Field:value} is used to find all documents that have field equals value. The form of {field1:value1, field2:value2} can be implemented and manipulated. ,,, and $lt $lte $gt $gte $ne respectively 小于、小于或等于、大于、大于或等于以及不等于 . For example, the command to find all male unicorns weighing more than 700 pounds is:

Db.unicorns.find ({Gender: ' m ', Weight: {$gt: 700}})//or (the effect is not exactly the same, only used in order to demonstrate different methods) Db.unicorns.find ({gender: {$ne: ' F '}, Weight: {$gte: 701}})

If necessary or not, you can use the $or operator and use it for an array that needs to be done or manipulated:

Db.unicorns.find ({gender: ' F ', $or: [{loves: ' Apple '}, {loves: ' orange '}, {weight: {$lt: 500}}]})

Go on ...

Pymongo Reference Sub-data
    • Pymongo Usage Summary
    • Pymongo Basic Use
    • pymongo_operate.py

Code snippet:

#首先pip Install Pymongo#连接mongodbImportPymongoConn=Pymongo.Connection(' localhost ',27017)#获取数据库列表Conn.Database_names()[U ' test1 ',U ' test2 ',U ' admin ',U ' Local ']#连接数据库Db=Conn.Test1#或者db = conn[' test1 ')#权限验证Db.Authenticate(' Username ',' Password ')True#获取聚集列表 (the concept of aggregation is similar to a table in a relational database)Db.Collection_names()[U ' account ',U ' role ',U ' item ',U ' online ']#连接聚集Account=Db.Account#或account = db[' account ']#查看聚集的一条记录Account.Find_one()#查看聚集的所有key (similar to fields in a relational database)Account.Find_one().Keys()#查看聚集的所有记录ForIInchAccount.Find():RintI#查看记录总数Account.Find().Count()#根据条件查询多条记录ForIInchAccount.Find({"Name":"XXX"}):PrintI#对查询结果进行排序 (default ascending ascending)Account.Find().Sort("Name",Pymongo.Ascending)Account.Find().Sort([("Name",Pymongo.Ascending),("Active_time",Pymongo.Descending)])#新增记录Account.Insert ({ "name"  "Mike"  "Active_time" :  "20130408" }) # Update record account. Update ({ "name" :  "Mike" }, { "$set" : { "Active_time" :  "20130408120000" }})  #删除记录 (without conditions for all deletions) account. Remove ({ "name" :  "Mike" })    

The

MONGO database.

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.