This is the 26th day of my participation in Gwen Challenge

preface

What is MongoDB?

MongoDB is an open source database system based on distributed file storage written in C++ language. MongoDB stores data as a document. The data structure consists of key=>value pairs. Belong to no.

What is NoSQL?

NoSQL refers to a non-relational database. Sometimes called Not Only SQL, NoSQL is used for storing very large amounts of data.

MongoDB has a lot of characteristics, here are the main points:

1. First, the programming languages it supports: RUBY, PYTHON, JAVA, C++, PHP, C#, etc.

2. You can set the index of any property

3. Mongo supports rich query expressions

4. Map/ Reduce in Mongodb is mainly used for batch processing and aggregation of data.

Download and install

www.mongodb.com/download-ce…

Management tool

RockMongo is recommended — lightweight and supports multiple languages.

1. Basic Concepts

There are many concepts in mongoDB that are very different from relational databases and need to be understood first.

Database: Multiple databases can be created. The default database is DB.

Collections: Just like tables in a relational database, collections are a combination of documents that can be inserted into different formats and types of data.

For example, insert the following documents into a collection. When a document is inserted, the collection is created.

{"title":"cat"}
{"age":5."name":"miaomiao"} 
{"age":6."name":"haha"."color":"white"}
Copy the code

Document: a set of key-value data in key-value pair format. Such as:

{"age":5."name":"miaomiao"}
Copy the code

Fields: Metadata, which can be thought of as data in a document.

Comparison with relational databases:

RDBMS MongoDB
The database The database
form A collection of
line The document
column field
Table joint The embedded document
A primary key Primary key (MongoDB provides key _id)

Second, MongoDB practical operation

Creating a database
use DATABASE_NAME
Copy the code

If there is a database, it will be switched over, and if there is no database, it will be created automatically.

Viewing all databases
> show dbs
Copy the code
Create a collection
db.createCollection(name, options)
Copy the code
  • Name: Name of the collection to be created
  • Options: Optional parameters that specify memory size and index options
> use test 
switched to db test 
> db.createCollection("animal") 
{ "ok" : 1 }
Copy the code
Inserted into the document
Db. COLLECTION_NAME. Insert (document) or the COLLECTION_NAME. Save (document)Copy the code

You can create a collection by inserting the document. The collection name is COLLECTION_NAME.

Db.collection.insertone (), which inserts a new document.

>db.col.insert({ "title" : "dog", "age" : 5, "color" : "white", "name" : "wanwan" })
Copy the code

Important: Key/value pairs in the document are ordered, type – and case-sensitive, and cannot have duplicate keys.

Query the document

The find () method.

db.collection.find(query, projection)
Copy the code

Projection is a key that needs to be returned. Projection is a key that needs to be returned. Projection is a key that needs to be returned. Some of mongoDB’s operators are needed here.

Correlation operator

Operator used for comparison

$gt – greater than

$lt – less than

$gte– is greater than or equal to

$LTE — less than or equal to

Conditional operator

And (and)

>db.col.find({key1:value1, key2:value2}).pretty()

And (or)

>db.col.find( { $or: [ {key1: value1}, {key2:value2} ] } ).pretty()

The instance

Now you have this document data.

> db.col.find() { “_id” : ObjectId(“56066542ade2f21f36b0313a”), “title” : “dog”, “age” : 5, “color” : “white”, “name” : “wanwan” }

{ “_id” : ObjectId(“56066542ade2f223420313a”), “title” : “cat”, “age” : 7, “color” : “white”, “name” : “miaomiao” }

{ “_id” : ObjectId(“56242342342f21f36b0313a”), “title” : “pig”, “age” : 2, “color” : “black”, “name” : “hoho” }

Below is the query document statement covering greater than, and or.

>db.col.find({"age": {$gt:5}, $or: [{"color": "white"},{"title": "pig"}]}).pretty() 
Copy the code

Query result:

{

"_id" : ObjectId("56066542ade2f223420313a"), 

"title" : "cat"."age" : "Seven"."color" : "white"."name" : "miaomiao" 

}
Copy the code
The sorting

Use the sort() method to specify the sort of parameters, 1 for ascending and -1 for descending.

>db.COLLECTION_NAME.find().sort({KEY:1})
Copy the code

Descending by age for documents in the above collection.

>db.col.find({},{"title":1,_id:0}).sort({"age":- 1}) 
Copy the code

Results:

{ "title" : "cat" } 
{ "title" : "dog" } 
{ "title" : "pig" } 
Copy the code