[MongoDB database of]

MongoDB

MongoDB or use C ++, is composed of key data, the data will look more like JSON (BSON), distributed support, support for the index. Sql query insert mode is not a type used in the mongo is a function of the way the query.

type of data:

string (character), Integer (whole), Boolean (Boolean), Double (floating point), Arrays (arrays), timestamp (timestamp), Null ( 'empty', the placeholder type), Object (json can set a json , embedded documents), object id (primary key type), Code (direct support javascript), binary data (binary), re (regular)

SQL term MONGO term Explanation
DATABASE DATABASE database
TABLE COLLECTION Table / collection
ROW DOCUMENT Line / Documents
COLUMN/FIELD FIELD Column / field
INDEX INDEX index
PRIMARY KEY PRIMARY KEY MONGOZ primary key is automatically generated (the _id)
TABLE JOIN NONE MONGO does not support inline table
MongoDB是一个非关系型(NoSQL)的数据库

MongoChef 是mongdb的图形可视化软件
  • mongodb operation
#普通的开启模式
mongod  #开启命令
#参数
--dbpath=路径   #指定数据库的存储路径
--logpath=路径.log  #指定的日子文件
--fork  #默认在后台启动(守护进程)
--config=/etc/mongod.conf  #指定使用配置文件打开

mongod --dbpath="C:/demo/mongo/data" --logpath="C:/demo/mongo/data/m.log" 

#创建一个简洁的打开方式
#创建一个.sh
在文件中写入:mongod --fork --config=/etc/mongod.conf
#开启mongodb
service mongod start

#进入mongodb中
mongo

#如果有密码
use admin  #用户验证库
db.auth("用户名","密码")   #显示1则成功

#关闭mongodb
service mongod stop

#关闭用户验证
security:
  authorization: disabled
  
#有些时候service mongod stop关闭不了
1.查看进程号:ps -aux|grep mongo
2.kill -9 进程号
#修改密码
use admin
#查看所有的用户信息
db.system.users.find().pretty()

db.system.users.remove({})

db.createUser({user:'root',password:'123456'})

Operation of the database

  • Create a database
1.use 数据库的名字   #当数据库中没有集合,集合中没有数据,默认不显示Bson
2.db.userinfo.insert({name:'李野',age:62,sex:'男',city:'东北'})
  • Delete Database
#选择好数据库
use 数据库
db.dropDatabase()
  • View database
show dbs
  • View current database used
db.getName()
db
  • Disconnect
1.exit
2.quit()
  • help
help

Set of operations

  • View all collections
show collections
  • Create Collection
1.语法:db.createCollection('集合的名字')
2.语法:db.集合名.insert({key:value})

#前者是创建了一个空的集合,后者是带有文档的集合
  • Delete Collection
db.集合的名字.drop()

Operation documents

  • Insert Document
1.插入单条
db.userinfo.insert({username:'tom',password:123456})

2.插入多条
db.userinfo.insert([{username:'tom1',password:123456},{username:'tom2',password:123456}])

3.save()
db.userinfo.save({name:'jack',age:90,sex:'男'})
db.userinfo.save([{name:'jack',age:90,sex:'男'},{name:'jack',age:90,sex:'男'}])
  • Update Documentation
#语法:
db.userinfo.update(
	{query},
    {update},
    {
    	{upsert:<boolean>}
    	{multi:<boolean>}
    	{writeConcern:<document>}
    }
    
)

#参数说明
query:update的条件,相当sql中where
update:$set(直接更新),$inc(在原有的基础累加)
upsert:可选项,如果记录不存在,是否当成新数据插入
multi:可选项,默认会更新第一条记录,默认值是false,如果为true,更新所有
writeConcern:可选,错误抛出的等级:

######################################################################
WriteConcern.NONE:没有异常抛出
WriteConcern.NORMAL:仅抛出网络错误异常,没有服务器错误异常
WriteConcern.SAFE:抛出网络错误异常、服务器错误异常;并等待服务器完成写操作。
WriteConcern.MAJORITY: 抛出网络错误异常、服务器错误异常;并等待一个主服务器完成写操作。
WriteConcern.FSYNC_SAFE: 抛出网络错误异常、服务器错误异常;写操作等待服务器将数据刷新到磁盘。
WriteConcern.JOURNAL_SAFE:抛出网络错误异常、服务器错误异常;写操作等待服务器提交到磁盘的日志文件。
WriteConcern.REPLICAS_SAFE:抛出网络错误异常、服务器错误异常;等待至少2台服务器完成写操作。
#######################################################################

#修饰器
$set:用来指定一个键的值,如果这个键不存在,则增加,如果存在直接修改
$inc:如果已经有了该键,直接累加
#name:jack的年龄改为18
db.userinfo.update({name:'jack'},{$set:{age:18}})

db.userinfo.update({name:'jack'},{$inc:{age:18}})

#修改所有的值
db.userinfo.update({name:'jack'},{$set:{age:18}},{multi:1})
db.userinfo.save(
	{document}
    {
    	{writeConcern:<document>}
    }
)

#save()执行更新,直接覆盖的过程,一定要加上_id,主要是覆盖原来的值
db.userinfo.save({name:'jack',age:18,sex:'male'})
  • Delete Document
#语法
db.集合名字.remove(
	{query},
    {
    	{justOne:<boolean>},
    	{writeConcern:<document>}
    }
)

#参数说明
query:当做必选,删除的条件
justOne:在mongodb中为True只会删除符合条件的一条数据,默认为false,删除全部


#删除name:jack的
db.userinfo.remove({name:'jack'},{justOne:true})
  • Inquire
#find()

#查询全部的数据
db.userinfo.find()

#格式输出
db.userinfo.find().pretty()

#查找一条
db.userinfo.findOne()

#带有条件的查询
db.userinfo.findOne({name:'tom'})
  • Conditional Operations
#大于 $gt
db.userinfo.find({age:{$gt:20}}).pretty()
#大于等于 $gte
db.userinfo.find({age:{$gte:20}}).pretty()
#小于 $lt
db.userinfo.find({age:{$lt:20}}).pretty()
#小于等于 $lte
db.userinfo.find({age:{$lte:20}}).pretty()
#不等于$ne
db.userinfo.find({age:{$ne:62}}).pretty()
#使用_id进行查询
db.userinfo.find({"_id" : ObjectId("5cac65bcb503068e96b400d2")}).pretty()
#统计
db.userinfo.find().count()
#正则
db.userinfo.find({name:/keyword/})
#以`困`字结尾的
db.userinfo.find({name:/keyword$/})
  • and和or
#and
db.userinfo.find({name:/keyword/,sex:'value'})
#$or
db.集合.find(
  {
  条件,
  $or:[{条件1},{条件n}]
  } 
)
#年龄
#select * from a where name='??' and (age=18 or age>=20);
db.userinfo.find({name:'??',$or:[{age:18},{age:62}]})
  • limit and skip
#limit() 
#skip() 跳过多少条
#跳过前两条
db.userinfo.find().skip(2)
#获取前两条
db.userinfo.find().limit(2)
#先略过前两条,在获取下两条
db.userinfo.find().skip(2).limit(2).pretty()
  • Sequence
#sort(条件)
#注意:升序1,降序是-1

db.userinfo.find().sort({age:-1}).skip(1).limit(2)

pymongo

pip install pymongo
from pymongo import MongoClient
from bson.objectid import ObjectId

#连接
conn = MongoClient(host='127.0.0.1',port=27017)

#选择数据库
db = conn.demo
#查询
res = db.userinfo.find({'_id':ObjectId("5cac65bcb503068e96b400d2")})

for i in res:
    print(i)
Published 116 original articles · won praise 10 · views 1368

Guess you like

Origin blog.csdn.net/weixin_44727383/article/details/104954249