A temporary need to use Python to manipulate MongoDB, online search found that many documents are already very old, do not conform to the current 3.0 version of the installation configuration, I want to tidy up to make a memo.
About MongoDB
MongoDB (http://www.mongodb.org/) is a high performance, open source (the generation of Earthquake army Daniel is studying MongoDB source code, we can go to see http://www.cnblogs.com/daizhj/), Mode Freedom ( Schema-free) is a document-based database that can be used in many scenarios to replace traditional relational databases or key/value (Key-value) storage methods. MongoDB uses C + + development,
Has the following characteristics:
Collection-oriented storage: suitable for storing objects and data in JSON form.
Dynamic query: MongoDB supports rich query expressions. Query directives use a JSON-style tag to easily query objects and arrays embedded in the document.
Full index support: Includes embedded objects and arrays in the document. The query optimizer of MongoDB parses the query expression and generates an efficient query plan.
Query monitoring: MongoDB contains a monitoring tool to analyze the performance of database operations.
Replication and automatic failover: The MongoDB database supports data replication between servers, supporting master-slave mode and inter-server replication. The primary goal of replication is to provide redundancy and automatic failover.
Efficient traditional storage: supports binary data and large objects such as photos or pictures.
Automatic sharding to support cloud-level scalability (in the early alpha phase): Automatic sharding supports a level of database clustering, adding additional machines dynamically.
Mode Freedom (schema-free) means that for files stored in a MongoDB database, we do not need to know any of its structure definitions. If necessary, you can store files of different structures in the same database.
Drivers for Python,php,ruby,java,c,c#,javascript,perl and C + + languages are supported, and drivers for Erlang and. NET platforms are also available in the community.
Use occasions:
Website data: MongoDB is ideal for real-time insertions, updates and queries, as well as the replication and high scalability required for real-time data storage on the site.
Caching: Because of its high performance, MongoDB is also suitable as a caching layer for the information infrastructure. After the system restarts, the persistent cache layer built by MongoDB avoids overloading the underlying data sources.
Large, low-value data: Storing some data in a traditional relational database can be expensive, and many times programmers often choose traditional files for storage.
Highly scalable scenario: MongoDB is ideal for databases consisting of dozens of or hundreds of servers. MongoDB's roadmap already includes built-in support for the MapReduce engine.
Storage for objects and JSON data: MongoDB's Bson data format is ideal for storing and querying in a document format.
The so-called "set-oriented" (collenction-orented), meaning that data is grouped in a dataset, is called a collection (collenction). Each collection has a unique identifying name in the database and can contain an unlimited number of documents. The concept of a collection is similar to a table in a relational database (RDBMS), unlike it does not need to define any schema (schema).
Follow the instructions in MongoDB's official website to install MongoDB using the. deb package, although Ubuntu 14.04 contains its own MongoDB package in its own repository. But we use the official. Deb package is usually updated in the same way. This Deb package contains the following packages:
Mongodb-org Metapackage will automatically install the following 4 components packages
Mongodb-org-server Mongod daemon and related configuration, init script, where/etc/init.d/mongod can be used to stop, start, and restart daemon processes
Mongodb-org-mongos MONGOs Daemon
Mongodb-org-shell MONGO Shell
Mongodb-org-tools MongoDB tools:mongoimport bsondump, Mongodump, Mongoexport, Mongofiles, Mongooplog, Mongoperf, Mong Orestore, Mongostat, and Mongotop.
Description
/var/lib/mongodb Storing data files
/var/log/mongodb Storing log files
/etc/mongod.conf configuration file
Use MongoDB's user account to run it. If you change the user, you need to ensure that it has access to the two files above
Installing MongoDB
1. Command line import MongoDB public GPG Key:
sudo apt-key adv--keyserver hkp://keyserver.ubuntu.com:80--recv 7F0CEB10
2. Generate MongoDB List file
echo "Deb Http://repo.mongodb.org/apt/ubuntu" $ (LSB_RELEASE-SC) "/mongodb-org/3.0 Multiverse" | sudo tee/etc/apt/sources.list.d/mongodb-org-3.0.list
3. Overloading the Local package database
sudo apt-get update
4. Install the latest stable version of MongoDB package
sudo apt-get install-y mongodb-org
5. Confirm the currently installed MongoDB version
echo "Mongodb-org hold" | sudo dpkg--set-selections
echo "Mongodb-org-server hold" | sudo dpkg--set-selections
echo "Mongodb-org-shell hold" | sudo dpkg--set-selections
echo "Mongodb-org-mongos hold" | sudo dpkg--set-selections
echo "Mongodb-org-tools hold" | sudo dpkg--set-selections
6. Start/stop/restart MongoDB
sudo service Mongod start
sudo service Mongod stop
sudo service mongod restart
View status
sudo service Mongod status
7. Check the log to see if MongoDB started successfully
Check if there is a log file in/var/log/mongodb/mongod.log
[Initandlisten] Waiting for connections on port <port>
Port defaults to 27017, which is configured in/etc/mongod.conf
At this point, MongoDB has been successfully installed and starts using it below. The Mongod daemon is database server, and the MONGO shell is available as a client and we go through it to interact with MongoDB.
8. Connecting Mongod
Mongo
Enter MONGO in the Ubuntu terminal, which will default to the database server listening on port 27017 on localhost, and if not, use the--port and--host options. Use Exit to exit the MONGO environment.
9. Select a Database
When MONGO is connected to Mongod, it uses the test database by default and uses the following operation to get information about it:
Help Display assistance information
DB View the currently connected database
Show DBS shows a list of all databases
Use mydb switch to mydb database
Note: MongoDB is not created immediately for a newly named database, and cannot be viewed in Show DBS unless you insert the data into it
10. Generate collection and insert documents
Db
To view the currently existing database, to create a new database, use the following command:
Use MyDB
Use the JS operation to generate two documents
j = {name: "MONGO"}
k = {X:3}
Insert J,k into the TestData collection, and note that MongoDB uses dynamic schemas, which does not need to declare testdata prior to inserting the data and is automatically generated when it is used.
Db.testData.insert (j)
Db.testData.insert (k)
When you insert the first document, both the MyDB database and the TestData collection are automatically generated.
11. Verify that TestData collection is present
Show collections
Verify that documents are present in the testdata
Db.testData.find ()
Will be returned
{"_id": ObjectId ("4c2209f9f3924d31102bd84a"), "name": "MONGO"}
{"_id": ObjectId ("4c2209fef3924d31102bd84b"), "X": 3}
12. Exit MONGO Shell
Exit
or press CTRL + C interrupt to exit
13. Set up additional client connections
The default installation, only allow 127.0.0.1 IP connection, need to modify the/etc/mongodb.conf,
sudo vim/etc/mongodb.conf
Comment out the record
#bind_ip = 127.0.0.1
Reference documents
[1].http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/
[2].http://docs.mongodb.org/manual/tutorial/getting-started/
[3].http://www.cnblogs.com/xiaogangqq123/archive/2011/05/16/2029426.html