MongoDB for Linux (1)

Source: Internet
Author: User
Tags mongodb server
If you want to talk about MongoDB, you must first talk about nosql. About nosql, two articles on the Internet are recommended: 1. http://www.iteye.com/topic/524977 by Fan Kai. This article details the background of nosql and the analysis of mainstream nosql products and their features. 2. http://sebug.net/paper/databases/nosql/Nosql.html by Yan Kai. This article introduces the basic idea, basic support theory, typical products and features, and current application cases of nosql. It is a comprehensive article in Chinese documents. Very recommended... About MongoDB introduction, You can Google, or directly go to the official website of MongoDB: http://www.mongodb.org/, here will not go into details. Next we will focus on two aspects: Installation and basic use (crud) I. MongoDB installation 1. install 1.1 in Linux and MACOs to create a data storage directory to store MongoDB data files. By default, this directory is/data/DB. Then grant the user the read and write permissions on the directory. You can change the owner of the directory. Enter the following command in the terminal: 1 mkdir-P/data/db2chown-r $ User: $ user/data/DB 1.2 download the compressed package from the official website and decompress it, and enter the directory after decompression (xxx represents the specific version number): 1tar zxf mongodb-linux-i686-XXX.tar.gz2cd mongodb-linux-i686-XXX 1.3 start Database: 1bin/mongod installation process is relatively simple. Ii. Basic MongoDB operations 2.1 Basic Concepts: Document: a series of key-value pairs are arranged in order to form a document. Document is one of the core concepts of MongoDB. The following is a document: 1 {"name": "wawlian", "Age": 23}. Note that the two key-value pairs above are ordered, in other words, the order cannot be disrupted. This is defined in the document. The document key is generally a string, but the value can be of many types, such as string, Boolean, and numeric. In addition, MongoDB is case sensitive. Set: a set of documents. Similar to tables in a traditional database, a set is similar to a record in a traditional database (rows in a table ). This is just an analogy in the early stage. The table of a set is different from that of a traditional database. The set has no mode. That is to say, the set does not require that the documents in the set have the same number of key-value pairs, and does not require that all keys be equal. This may be a bit abstract. Let's look at an example: 1 {"name": "wawlian", "Age": 23} 2 {"language ": "Chinese"} the two forms of completely different documents are allowed to exist in a set. Database: the concept of a database is similar to that of a database or schema in Oracle. Multiple different sets form a database. A MongoDB instance can load multiple different databases, which are completely independent of each other. 2.2 Before starting and connecting to MongoDB to start MongoDB, we have already said in the installation, just enter: 1. /mongod normally starts output like this, of course there may be a slight gap between different versions: 1thu Dec 1 22:49:22 [initandlisten] MongoDB starting: PID = 281 Port = 27017 dbpath =/data/DB/64-bit host = jerrymacbookhome. local2thu Dec 1 22:49:22 [initandlisten] DB version v2.0.1, pdfile version 4.53thu Dec 1 22:49:22 [initandlisten] git version: 3a5cf0e2134a830d38d2d1aae7e88cac31bdd6844thu Dec 1 22:49:22 [in Itandlisten] Build info: Darwin erh2.10gen. CC 9.6.0 Darwin kernel version 9.6.0: Mon Nov 24 17:37:00 PST 2008; root: xnu-1228.9.59 ~ 1/release_i386 i386 boost_lib_version = listen 405thu Dec 1 22:49:22 [initandlisten] Options: {} 6thu Dec 1 22:49:22 [initandlisten] journal dir =/data/DB/journal7thu Dec 1 22:49:22 [initandlisten] RECOVER: No journal files present, no recovery needed8thu Dec 1 22:49:23 [websvr] admin Web Console waiting for connections on port 28017 is connected to the MongoDB server. We know that MySQL can connect to the MySQL server through the 1 mysql-uroot-p command, and Oracle can also use 1 sqlplus user/passwd, MongoDB also has such a tool. MongoDB provides a javascript shell that can interact with MongoDB. Run the shell: 1./The output of the successful Mongo connection is as follows: 1 jerrymacbookhome :~ Jerry $ mongo2mongodb shell version: 2.0.13connecting to: test shell is a javascript interpreter that can run any JavaScript program. Of course, this is not the focus of our attention. We focus on how to complete basic CRUD operations in MongoDB. 2.3 Before performing basic CRUD operations, you need to add some basic knowledge. MongoDB uses a variable dB to store the currently used database. We connect to the MongoDB database. By default, we connect to the test database. To switch to another database, run the use dbname command: 1> Use foobar2switched to DB foobar 2.3.1 create: create assume that you need to save the personnel information (name, age, and date) in the persons collection ). You can create a JS object and insert it into the corresponding set of the database: 1> P = {"name": "wawlian", "Age": 23, "cdate ": new Date ()}; 2 {3 "name": "wawlian", 4 "Age": 23,5 "cdate": isodate ("2011-12-01t17: 13: 45.671z ") 6} 7> dB. persons. insert (p); the portion of the above {} is the output of the previous command. The personnel information has been saved. In addition, we didn't create a set of persons before. This was created by MongoDB itself when we saved the personnel information. 2.3.2 retrieve: Query all records by calling the find () method of the set: 1> dB. persons. find (); 2 {"_ id": objectid ("4ed7b5ce4f33e90801872724"), "name": "wawlian", "Age": 23, "cdate ": isodate ("2011-12-01t17: 13: 45.671z")} the records we inserted earlier have been viewed. We found that an additional key-Value Pair: _ ID/objectid. This is the unique identifier that MongoDB adds for each document. 2.3.3 update: Update updates use the update () method of the set. Of course, to update the database, let the database know who to update. Therefore, this method has two parameters: the update condition and the new document. View the Code directly: 1> dB. persons. update ({"name": "wawlian" },{ "name": "Xiaobai", "Age": "18"}); 2> dB. persons. find (); 3 {"_ id": objectid ("4ed7b5ce4f33e90801872724"), "name": "Xiaobai", "Age ": "18"} the above Code replaces "name" with "wawlian" with the last one. 2.3.4 Delete: to delete a document, you only need to use the remove () method of the set. The remove () method requires passing a parameter: the deletion condition. This is similar to the update () method above. 1> dB. Persons. Remove ({"name": "Xiaobai"}); 2> dB. Persons. Find (); after deletion, check that the record is gone. The above is an introduction to MongoDB. As I continue to learn, I will further introduce 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.