MongoDB Learning Notes (i)

Source: Internet
Author: User
Tags mongodb version

Some time recently, I have been working on MongoDB, I used to use a relational database, such as Oracle, MySQL, for MongoDB is only a little superficial understanding, recently determined to do a good study, the main reference books have two: "MongoDB Big Data Processing authoritative Guide (second edition), "MongoDB authoritative Guide (first edition)", the latter version of the older, but the basic can be used, the basic mode of operation is basically similar. My main reference is the first book, relatively speaking, the first book is relatively simple, but the entry is relatively easy, but also with PHP and Python operation of MongoDB Guide.

1, Installation: MongoDB installation is relatively simple, the main station is www.mongodb.org, both 32-bit version, there are 64-bit version, try to use the 64-bit version.

In the Windows environment, you can download the corresponding installation package and install it

Linux can download the package, if you use Ubuntu or fedora, you can also download the installation through the online archive, which can be installed to the latest version, the specific installation content can be referenced https://docs.mongodb.org/manual/ Administration/install-on-linux/, this is the official MongoDB document, if there is any problem, you can check here, more authoritative, the content is more complete.

The environment I install and test is as follows

Windows 10 Professional Edition

MongoDB version 3.0.6, installation path is D:\Program Files\mongodb

To facilitate the use of MongoDB, add its command directory to the environment variable path, the default command is saved in D:\Program Files\mongodb\server\3.0\bin

2, MongoDB compared to Oracle, MySQL, the structure is much simpler, as with Oracle, one instance can have multiple databases, the database can be any number of collection,collection similar to the concept of the table in Oracle, But there is the essence of the collection, each row in the store is a key-value pair, called document, which is also a reason for MongoDB called documents-oriented data, if you are familiar with JSON, understand this is very fast, the key is easy to understand, can be understood as the table column name, But its value is more special, can be an array, can also be a key value pair, and collection key is very free, the documnet under it can have different keys, in short, documnet although belong to a collection, but its key value pair is unlimited, If you want to do it, just do it. This means of storage, both its advantages and disadvantages.

Advantages: (Relative relational database)

1) due to the simple way of storage, cuid speed is very fast, there is such a sentence: MongoDB if the query over 1 seconds, there must be a problem

2) There is no hard constraint between the document under the same collection, there is no constraint between different collection, so there is no integrity requirement for the relational database, which is very important for replication and sharding.

3) replication, sharding relative relational database, it is much easier, can be scaled to improve the performance of the system.

Disadvantage: (relative to relational database)

1) because there is no integrity constraints, so the normative data is more difficult, need good programming discipline and error-checking means to constrain

2) Lack of support for transactions, not suitable for use in financial systems

3) Lack of collection function (perhaps in the future), many relational databases require a very inconvenient operation, generate reports need to use MapReduce.

In short, MongoDB's performance, expansion, replication, sharding has unique advantages, so it is not suitable for enterprise application development, but it is very suitable for the development of Internet, cloud-related applications, so need to choose the right range to use it.

3. Simple operation

1) Start

The command to start MongoDB from the command line is Mongod, you need to specify the port and database data path, set up the database directory D:\MONGODB_DATA\DB1 in this machine, the port is 27017

Mongod--dbpath d:\mongodb_data\db1--port 27017

2) connection

MONGODDB provides a command-line interface MONGO, which is a JavaScript terminal that can manipulate the database through JavaScript scripts

D:\>mongo 192.168.1.223:27017
MongoDB Shell version:3.0.6
Connecting To:192.168.1.223:27017/test
>

(192.168.1.223 is the native IP, if you connect remote MongoDB, you need to modify the IP address)

3) Select Database

The default is to connect to the test database, we create a new database, such as SHIYQ, as follows

> Use SHIYQ

(Note that the use is the command to switch the database, if there is no database, the system will create a default, and the database name is case-sensitive, so with this command to be more careful)

> Use SHIYQ
Switched to DB Shiyq
> DB
Shiyq
> Show DBS
Local 0.078GB
Test 0.078GB
TestDB 0.078GB
> Show Collections;
>

You can see that the DB command is showing the current database, show DBS is showing which databases are in this instance, because SHIYQ is a new database, there is no content, so it is not displayed, show collections shows the collection name in this database, not currently, So there is no display.

4) Create document

> Db.students.insert ({name: ' Wang Qiang ', Code: ' S101-1 ', AGE:15});
writeresult ({"ninserted": 1})
> Db.students.insert ({name: ' Liu Huan ', Code: ' S101-2 ', age:18});
writeresult ({"ninserted": 1})
> Db.students.insert ({name: ' Zhao ', Code: ' S101-3 ', age:17});
writeresult ({"ninserted": 1})
> Db.students.insert ({name: ' Zhao ', Code: ' S101-4 ', age:19});
writeresult ({"ninserted": 1})
> Db.students.insert ({name: ' Zhao ', Code: ' S101-5 ', age:19});
writeresult ({"ninserted": 1})

Collection name is students, you can see collection is not required to create, as long as the creation of the document under it can be;

> Db.students.find ()
{"_id": ObjectId ("560b95bd3bd6389af11ee3d0"), "name": "Wang Qiang", "Code": "S101-1", "Age": 15}
{"_id": ObjectId ("560b961c3bd6389af11ee3d1"), "name": "Liu Huan", "Code": "S101-2", "Age": 18}
{"_id": ObjectId ("560b96353bd6389af11ee3d2"), "name": "Zhao", "Code": "S101-3", "Age": 17}
{"_id": ObjectId ("560b963c3bd6389af11ee3d3"), "name": "Zhao", "Code": "S101-4", "Age": 19}
{"_id": ObjectId ("560b96413bd6389af11ee3d4"), "name": "Zhao", "Code": "S101-5", "Age": 19}

You can see the data that has been inserted, you need to note that each document has a _id field, which is equivalent to the collection primary key, this key value can also be specified manually, if the system is specified, you can ensure that there is no conflict in replication, sharding.

If you use Nodejs, or if you are familiar with JavaScript, the above content is very easy to understand.

5) Update

Syntax: Db.collection.update (criteria,objnew,options)

Criteria is the filter condition, Objnew is the new content, options={upsert:true,multi:true},upsert=true, if there is an update, otherwise created, multi=true, if more than one data is met, then all updates, The default is to update only the first (note that if you do not specify a key, you cannot use multi=true), it is important to note that the objnew here will completely replace the original content, if you want to modify a single key value, you cannot use this method.

> db.students.update ({name: ' Wang Qiang '},{grade:1});
Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})
> Db.students.find ());
{"_id": ObjectId ("560b95bd3bd6389af11ee3d0"), "Grade": 1}

6) Delete:

> Db.students.remove ({grade:1});
Writeresult ({"nremoved": 1})

There are plenty of other things to do, like

Db.collection.save (), with _id Update, no new

$inc Add Value

$set Modify a single key value

$unset Delete a key value

$push add a value to the array key

$pull Remove all from the array key value

$pullAll remove multiple values from an array

$pop Delete the last value in the array

The same syntax for the above $ is db.collection.update (criteria,{$set: {Author: ' Shiyq '});

MongoDB Learning Note (i)

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.