Beginner MongoDB Practice Notes-Install, create databases, save and query data

Source: Internet
Author: User
Tags mongodb query

MongoDB is a scalable, high-performance, distributed document storage database, written in C, designed to provide scalable, high-performance data storage solutions for Web applications. It is characterized by high performance, easy to deploy, easy to use, and easy to store data.

Mongo DB is a non-relational database (NOSQL) that is currently very popular in the IT industry, and its flexible data storage methods are highly favored by current it practitioners. Mongo DB is a good implementation of object-oriented thinking (Oo idea), in Mongo db each record is a document object. The biggest advantage of Mongo DB is that all data persistence requires no developers to write SQL statements manually, and it is easy to invoke methods to implement CRUD operations.

Introduction to the Documentation database:

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

As shown in the MongoDB document structure:

The advantages of using a document database are as follows:

In many programming languages, documents (that is, objects) fit into the native data type;

Embedded documents and arrays reduce the need for expensive relationship-related associations;

Dynamic Data structure mode supports smooth, scalable polymorphism.

Installation

Official website: http://www.mongodb.org/downloads, download the Windows 64bit address.

MongoDB installation on Windows 7 is easy to run. Download, unzip, and then run Bin/mongod to start the server and run Bin/mongo to run the command line client.

I am using the default installation to the C drive Program Files\mongodb 2.6 Standard directory, in order to facilitate learning, copy it to the C packing directory, for C:\MongoDB.

Note: It is best not to install to the C-Drive program Files directory, and the installation directory does not contain spaces, otherwise, will be troublesome, that is, the command line parameters each parameter should be "", for example:

Repeat "I am hungry" now

The command assigns the string "I am Hungry" to argv[1] and assigns the string "Now" to argv[2].


Before we start MongoDB, we must create a new directory that holds MongoDB data and logs. Database directory: C:\MongoDB\data\db\, log directory: C:\MongoDB\data\.


Start the service

Open the cmd window and go to the C:\MongoDB\bin directory and run the server Mongod.exe.

C:\mongodb\bin>mongod.exe--dbpath=c:\mongodb\data\db--directoryperdb--logpath=c:\mongodb\data\logs-- Logappend


Note: If the service does not start successfully, there are mainly two reasons, one is the Data\db\ directory is not built, and the firewall does not allow the port required to open the service.


Running the client

Then open a CMD window and go to the C:\MongoDB\bin directory and run the client Mongo.exe to login to MongoDB. (the window to keep the server mongod.exe is not closed)


Java Development Database Driver

Drive the jar package link address to drive the ZIP package link address. Https://github.com/mongodb/mongo-java-driver/releases


Practice on the client

Delete User: Db.dropuser (' username ')

Create an OA database: Use OA

Note: If you do not do anything else, the OA database will not be created.

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

To view the database:

> Show DBS
OA 0.078GB
Admin 0.078GB
db (empty)
Local 0.078GB
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.doctest.save ({id:1,name: ' Ttest1 '});

Writeresult ({"ninserted": 1})

> Db.doctest.save ({id:2,name: ' Ttest1 ', Code: ' 102 '});

Writeresult ({"ninserted": 1})

> Db.doctest.save ({id:3,name: ' Ttest3 ', Code: ' 103 ', class: ' Doc '});

Writeresult ({"ninserted": 1})

> Db.doctest.save ({id:4,name: ' Ttest4 ', Code: ' 104 '});

Writeresult ({"ninserted": 1})


Inquire

Number of query data (count)

> Db.doctest.find (). Count ();

4

condition (=) query, the condition is: name= "Ttest1".

> Db.doctest.find ({"Name": "Ttest1"});

{"_id": ObjectId ("54a1003556a081db9d632745"), "id": 1, "name": "Ttest1"}

{"_id": ObjectId ("54a1005756a081db9d632746"), "id": 2, "name": "Ttest1", "Code": "102"}

Condition (>=) query, the condition is: id>3.

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

{"_id": ObjectId ("54a100a056a081db9d632748"), "id": 4, "name": "Ttest4", "Code": "104"}

> Db.doctest.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.doctest.find ({id:{$in: [2,3]}});

Description: $gt: >-(first letter of Greater than)

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

$lt:<-(the first letter of less than)

$lte: <=-(the first letter of less than or equal)

$ne:! =-(not equal's first letter)


Recommended Client Tools

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


Database design

MongoDB no fixed structure, each table each piece of data can have a different structure, which is both a good and a disadvantage, the disadvantage is that you have to understand the structure of the table MongoDB, which in fact to the maintenance personnel to bring a certain discomfort and trouble.

This problem is also easy to solve, that is, to increase the table structure definition table, to illustrate the various situations of the structure of the definition, such as configurable approval order, is more applicable.

A nested design, such as an approval order with a line item, where the detail item is a multiline data content, the operation is as follows:

>db.doctest.save ({id:5,name: ' Ttest5 ', Code: ' 106 ', Detail:[{item: ' Test card 1 ', type: ' Card ', Acount:3},{item: ' Test Card 2 ', Type: ' Card ', Acount:5}]});

Query which model (type) is the "Mobile" record, in the following operation:

> Db.doctest.find ({"Detail.type": "Mobile"});
{"_id": ObjectId ("54a39ebdd8389293ac59e78a"), "id": 6, "name": "Ttest6", "Code": "107", "detail": [{"Item": "Employee Card 1 "," type ":" Card "," Acount ": 3}, {" Item ":" Test Phone "," type ":" Mobile "," Acount ": 5}]}

To view the design of an approval order:

> Db.doctest.find ();



Querying inline documents

There are two ways to query a document, one is a full query, the other is a query against a key value pair! The exact match query for the inline document and the exact matching query for the array, the number of key-value pairs in the inline document must be consistent before matching, as in the following example:

Queries for embedded document-specific key-value pairs are most commonly used! The key for the embedded document is represented by dot notation.


Reference: Introduction to MongoDB

Learning mongodb--(4-2): MongoDB queries (arrays, inline documents, and $where)

MongoDB Query Statement Learning Summary

Baidu Encyclopedia MongoDB

Big Data maker Alliance MongoDB

Beginner MongoDB Practice Notes-Install, create databases, save and query data

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.