The basic syntax of MongoDB

1. Introduction

MongoDB is a database based on distributed file storage. Written in C++ language. Designed to provide scalable high-performance data storage solutions for WEB applications.
MongoDB is a product between relational and non-relational databases. It is the most versatile and most like relational database among non-relational databases.
MongoDB stores data as a document, and the data structure consists of key-value (key=>value) pairs. MongoDB documents are similar to the BSON format of JSON objects. The field value can include other documents, arrays and document arrays.

Insert picture description here

Features of MongoDB

The biggest feature of MongoDB is that the query language it supports is very powerful. Its syntax is a bit similar to an object-oriented query language. It can almost achieve most of the functions similar to single-table queries in relational databases, and it also supports
indexing of data . It is a collection-oriented, document-based database with free patterns.
The specific features are summarized as follows:
(1) Collection-oriented storage, easy to store object type data
(2) Free mode
(3) Support dynamic query
(4) Support full index, including internal objects
(5) Support replication and failure recovery
(6) Use efficient binary data storage, including large objects (such as videos, etc.)
(7) Automatically process fragments to support cloud computing level scalability
(8) Support Python, PHP, Ruby, Java, C, C#, Javascript, Perl and C++ language driver
(9) The file storage format is BSON (an extension of JSON)

MongoDB architecture

Insert picture description here

MongoDB data types

Insert picture description here
Basic data type

null: Used to represent a null or non-existent field, {"X":null}
Boolean type:
Boolean type has two values ​​true and false, {"x": true}
Value: The
shell uses 64 as a floating point type by default Numerical value. {"X": 3.14} or {"x": 3}.
Floating point type:
For integer values, you can use NumberInt (4-byte signed integer) or NumberLong (8-byte signed integer), {"x":NumberInt("3")}, {"x":NumberLong(" 3")}
String:
UTF-8 strings can be expressed as string type data, {"x": "Hehe"}
Date: The
date is stored as the number of milliseconds that have passed since the new era, and the time zone is not stored. { "X":new Date()}
Regular expression: When
querying, use regular expression as the qualification, the syntax is the same as JavaScript regular expression, {"x": /[abc]/}
array:
data list or data Set can be expressed as an array, {"x": ["a", "b", "c"]}
Embedded documents:
documents can be nested with other documents, and the nested documents are treated as values, {"x" :{"Y":3 }}
Object Id:
Object id is a 12-byte string, which is the unique identification (primary key) of the document, {"x": objectId() }
Binary data:
Binary data is an arbitrary word The string of the section. It cannot be used directly in the shell. If you want to save non-utf-characters to the database, binary data is the only way.
Code:
Any JavaScript code can be included in queries and documents, {"x":function(){/.../}}

Second, install MongoDB

Install using Docker

# 拉取镜像
 docker pull mongo:3.6
 # 查看镜像
 docker images
 # 创建容器
docker run --name mongo -v ~/docker/mongo:/data/db -p 27017:27017 -d mongo:3.6  
 # 查看容器
 docker ps 
 # 进入容器
 docker exec -it mongo /bin/bash

Modify the configuration file and open the remote connection

docker cp mongo:/etc/mongod.conf.orig /data/

将其中的
bindIp: 127.0.0.1
注释掉# bindIp: 127.0.0.1
或者改成bindIp: 0.0.0.0
即可开启远程连接



docker cp /data/mongod.conf.orig mongo:/etc/mongod.conf.orig 

Insert picture description here
Insert picture description here
Connect using graphical tools
Insert picture description here

Three, grammar

Summary: The
collection name is equivalent to the table name

# 插入
db.集合名称.insert(数据)
#查找全部
db.集合名称.find()
#查询一个
db.集合名称.findOne({
    
    userid:'01'})
db.集合名称.find().limit(3)
# 修改文档
db.集合名称.update(条件,修改后数据)
# 删除全部文档
db.集合名称.remove({
    
    })
# 删除指定文档
db.集合名称.remove({
    
    _id:'1'})
# 统计条数
db.集合名称.count()
# 根据条件统计条数
db.集合名称.count({
    
    content:'内容'})
# 模糊查询   MongoDB的模糊查询是通过正则表达式的方式实现的。格式为:/模糊查询字符串/
db.集合名称.find({
    
    字段名称:/bug/})
# 匹配content中以‘太’开头的数据
db.集合名称.find({
    
    字段名称:/^/})
# 大于 等于 小于
使用这些需要单独将该值再放入一个{
    
    }中
db.集合名称.find({
    
     字段名称 : {
    
     $gt: value }}) // 大于: field > value
db.集合名称.find({
    
     字段名称 : {
    
     $lt: value }}) // 小于: field < value
db.集合名称.find({
    
     字段名称 : {
    
     $gte: value }}) // 大于等于: field >= value
db.集合名称.find({
    
     字段名称 : {
    
     $lte: value }}) // 小于等于: field <= value
db.集合名称.find({
    
     字段名称 : {
    
     $ne: value }}) // 不等于: field != value
//查询id字段在1和2
db.集合名称.find({
    
    字段名称:{
    
    $in:["1","2"]}})
//查询id字段不在1和2
db.集合名称.find({
    
    字段名称:{
    
    $nin:["1","2"]}})
//查询访问量大于等于1000 且小于2000的数据
db.集合名称.find({
    
    $and:[ {
    
    字段名称:{
    
    $gte:1000}} ,{
    
    字段名称:{
    
    $lt:2000} }]})
db.集合名称.find({
    
    $or:[ {
    
    字段名称:{
    
    $gte:1000}} ,{
    
    字段名称:{
    
    $lt:2000} }]})
//列值增长
db.集合名称.update({
    
    字段名称:"2"},{
    
    $inc:{
    
    visits:NumberInt(2)}})

1. Select and create a database

use 数据库名称   # 如果数据库不存在,就自动创建

例如 创建并使用spitdb
use spitdb

Insert picture description here
Insert picture description here

2. Insert and query documents

The following command will automatically create a collection, which is what we often call a table.

db.集合名称.insert(数据);   #  数据格式是BSON格式  
db.spit.insert({
    
    content:"java是第一编程语言",userid:"1011",nickname:"万能的张三",visits:NumberInt(902)})

Insert picture description here
Insert picture description here
Query all data:

Insert picture description here
Insert picture description here
The _id field appears in the query result, and the data format is ObjectID (234123as1234), which is automatically generated by MongoDB. We can also specify the id to insert, but the id field must be _id

db.spit.insert({
    
    _id:"1",content:"有个bug求解决",userid:"1012",nickname:"张三",visits:NumberInt(2020)});
db.spit.insert({
    
    _id:"2",content:"分析面试经验",userid:"1013",nickname:"李四",visits:NumberInt(1023)});
db.spit.insert({
    
    _id:"3",content:"想拿高薪私聊我",userid:"1013",nickname:"王五",visits:NumberInt(111)});
db.spit.insert({
    
    _id:"4",content:"嘿嘿嘿",userid:"1014",nickname:"赵六",visits:NumberInt(1223)});
db.spit.insert({
    
    _id:"5",content:"我太难了",userid:"1016",nickname:"周期",visits:NumberInt(1255)});

Insert picture description here
Insert picture description here

3. Query by condition

//查询全部
db.spit.find()
//根据id查询   条件是bson格式
db.spit.find({
    
    _id:'1'})
//如果有多条记录返回,使用findOne命令只返回一条
db.spit.findOne({
    
    _id:'1'})

Insert picture description here
Insert picture description here

4. Return the specified number of data

Insert picture description here

5. Modify the document

db.spit.find({
    
    _id:'1'})

Insert picture description here
Insert picture description here

Note that only this field will be saved after modification.

Use $set to modify the specified field, pay attention to the colon after it:

db.spit.update({
    
    _id:'1'},{
    
    $set:{
    
    visits:NumberInt(20000)}})

6. Delete documents

//删除文档
db.spit.remove({
    
    _id:'11'})

Insert picture description here

7. Statistics

//统计条数
db.spit.count()
db.spit.count({
    
    visits:902})

Insert picture description here
Insert picture description here

8. Fuzzy query

//模糊查询
db.spit.find({
    
    content:/bug/})
db.spit.find({
    
    content:/^j/})

Insert picture description here

9. Greater than less than, and, or, in, nin, inc

//访问量大于1000
db.spit.find({
    
    visits:{
    
    $gt:1000}})
//访问量小于1000
db.spit.find({
    
    visits:{
    
    $lt:1000}})
//访问量大于等于1000
db.spit.find({
    
    visits:{
    
    $gte:902}})
//访问量小于等于1000
db.spit.find({
    
    visits:{
    
    $lte:902}})
//访问量不等于1000
db.spit.find({
    
    visits:{
    
    $ne:902}})
//访问量等于1000
db.spit.find({
    
    visits:{
    
    $eq:902}})
//查询id字段在1和2
db.spit.find({
    
    _id:{
    
    $in:["1","2"]}})
//查询id字段不在1和2
db.spit.find({
    
    _id:{
    
    $nin:["1","2"]}})
//查询访问量大于等于1000 且小于2000的数据
db.spit.find({
    
    $and:[ {
    
    visits:{
    
    $gte:1000}} ,{
    
    visits:{
    
    $lt:2000} }]})
db.spit.find({
    
    $or:[ {
    
    visits:{
    
    $gte:1000}} ,{
    
    visits:{
    
    $lt:2000} }]})
//列值增长
db.spit.update({
    
    _id:"2"},{
    
    $inc:{
    
    visits:NumberInt(2)}})

Guess you like

Origin blog.csdn.net/DreamsArchitects/article/details/109352830