MongoDB Practice Notes for beginners-install, create databases, save and query data, and learn mongodb

Source: Internet
Author: User
Tags mongodb query

MongoDB Practice Notes for beginners-install, create databases, save and query data, and learn mongodb

MongoDB is a scalable and High-Performance Distributed Document storage database written in C. It is designed to provide scalable and high-performance data storage solutions for web applications. It features high performance, ease of deployment, and ease of use, making it easy to store data.

Mongo DB is a non-relational database (NoSql) that is very popular in the IT industry. Its flexible data storage method is favored by current IT practitioners. Mongo DB implements the object-oriented idea (OO idea), and every record in Mongo DB is a Document object. The biggest advantage of Mongo DB is that all data persistence operations can easily implement CRUD operations without the need for developers to manually write SQL statements.

Document Database introduction:

A record in a MongoDB database is a document whose data structure consists of (field) and (value) pairs. MongoDB documents are similar to JSON objects. The field value can contain other documents, arrays, and document arrays.

Shows the MongoDB document structure:

The advantages of using the document database are as follows:

In many programming languages, documents (objects) are suitable for native data types;

Embedded documents and arrays reduce expensive relational association needs;

The dynamic data structure mode supports smooth and scalable polymorphism.

Install

Official Website: http://www.mongodb.org/downloads, Download Windows 64bit address.

MongoDB is easy to install and run on Windows 7. Download and decompress the package directly, run bin/mongod to start the server, and run bin/mongo to run the command line client.

I installed it in the C-Drive Program Files \ MongoDB 2.6 Standard Directory by default. For convenience of learning, copy it to the C-drive root directory, which is C: \ MongoDB.

Note: Please do not install it in the C drive Program Files directory, and the installation directory should not contain spaces. Otherwise, it will be troublesome, that is, to enclose each parameter of the command line parameters, for example:

Repeat "I am hungry" now

The command will allocate the string "I am hungry" to argv [1] and the string "now" to argv [2].


Before starting MongoDB, we must create a directory to store mongoDB data and logs. Database Directory: C: \ MongoDB \ data \ db \, log directory: C: \ MongoDB \ data \.


Start the service

Open the CMD window, go to the C: \ MongoDB \ bindirectory, and run cmd.exe on the service end.

C: \ MongoDB \ bin> mongod.exe -- dbpath = C: \ MongoDB \ data \ db -- directoryperdb -- logpath = C: \ MongoDB \ data \ logs -- logappend


Note: If the service is not started successfully, there are two main reasons: first, the data \ db \ directory is not created, and the firewall does not allow open ports required for the service.


Run the client

Open a CMD window, go to the C: \ MongoDB \ bindirectory, and run the client terminal cmd.exe to log on to MongoDB. (Do not close the window of cmd.exe on the service end)


Java Development Database driver

The link address of the driver Jar package and the link address of the driver ZIP package. Https://github.com/mongodb/mongo-java-driver/releases


Practice on the client

Delete A User: db. dropUser ('username ')

Create an OA Database: use OA

NOTE: If no other operations are performed, the OA database will not be created.

Create collections: OA. createCollection ("mytest ");

View the database:

> Show dbs
OA 0.078 GB
Admin 0.078 GB
Db (empty)
Local 0.078 GB
Test (empty)

View Collection (equivalent to "table "):

> Show collections


Create a document data table and insert data records.

> Use OA

Switched to db OA

> Db. createCollection ("doctest ")

{"OK": 1}

> Db.doc test. save ({id: 1, name: 'ttest1 '});

WriteResult ({"nInserted": 1 })

> Db.doc test. save ({id: 2, name: 'ttest1', code: '20140901 '});

WriteResult ({"nInserted": 1 })

> Db.doc test. save ({id: 3, name: 'ttest3 ', code: '123', class: 'Doc '});

WriteResult ({"nInserted": 1 })

> Db.doc test. save ({id: 4, name: 'ttest4', code: '20170101 '});

WriteResult ({"nInserted": 1 })


Query

Count)

> Db.doc test. find (). count ();

4

Condition (=): name = "ttest1 ".

> Db.doc test. find ({"name": "ttest1 "});

{"_ Id": ObjectId ("54a1003556a081db9d632745"), "id": 1, "name": "ttest1 "}

{"_ Id": ObjectId ("54a1005756a081db9d632746"), "id": 2, "name": "ttest1", "code": "102 "}

Condition (> =): id> 3.

> Db.doc test. find ({id: {$ gt: 3 }});

{"_ Id": ObjectId ("54a100a056a081db9d632748"), "id": 4, "name": "ttest4", "code": "104 "}

> Db.doc test. find ({id: {$ gte: 3 }});

{"_ Id": ObjectId ("54a1008c56a081db9d632747"), "id": 3, "name": "ttest3", "code": "103", "class ": "doc "}

{"_ Id": ObjectId ("54a100a056a081db9d632748"), "id": 4, "name": "ttest4", "code": "104 "}

Condition (in) query with the condition id in (2, 3 ).

> Db.doc test. find ({id: {$ in: [2, 3]});

: $ Gt:> -- (the first letter of Greater)

$ Gte: >=-- (the first letter of Greater than or equal)

$ Lt: <-- (first letter of Less)

$ Lte: <= -- (the first letter of Less than or equal)

$ Ne :! = -- (First letter of Not equal)


Recommended client tools

1. MongoVUE, http://blog.mongovue.com/


Database Design

MongoDB does not have a fixed structure. Each table can have a different structure. This is both a benefit and a disadvantage because you must understand the MongoDB table structure, this actually brings some discomfort and trouble to the maintenance personnel.

This problem can also be easily solved by adding a table structure definition table to describe the structure definitions in various situations. For example, you can configure an approval form.

Nested design. For example, if the approval form contains a detailed project, where the detailed project is a multi-row data content, the operation is as follows:

> Db.doc test. save ({id: 5, name: 'ttest5', code: '000000', detail: [{item: 'test Card 1', type: 'card', acount: 3}, {item: 'test Card 2', type: 'Card ', acount: 5}]});

Query the records whose model (Type) is "Mobile". The operation is as follows:

> Db.doc test. find ({"detail. type": "Mobile "});
{"_ Id": ObjectId ("54a39ebdd8389293ac59e78a"), "id": 6, "name": "ttest6", "code": "107", "detail ": [{"item": "employee Card 1", "type": "Card", "acount": 3 },{ "item": "Test mobile phone", "type ": "Mobile", "acount": 5}]}

View the approval form design:

> Db.doc test. find ();



Query embedded documents

There are two ways to query a document. One is to query the document completely, and the other is to query the key-value pair! The exact match query of the embedded document is the same as the exact match query of the array. The number of key-value pairs in the embedded document must be consistent in order to match, as shown in the following example:

Queries for specific key-value pairs in embedded documents are the most common! Vertices are used to accurately represent the keys of embedded documents.


Reference: Introduction to MongoDB

Learning MongoDB -- (4-2): MongoDB query (array, embedded documentation, and $ where)

Mongodb query statement learning Abstract

Baidu encyclopedia MongoDB

Big Data vendor Alliance 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.