MongoDB Basic Operations 1 additions and deletions to check the article

Source: Internet
Author: User

MongoDB Logical Storage structure
MongoDB的逻辑只要由文档(document)集合(collection)数据库(database)三部分组成。其中文档的是MongoDB核心概念,它是MongoDB逻辑存储最小的单元相当与关系型数据库中的一行记录,多个文档组成了集合,集合相当于关系数据库中的表的概念,多结合组成了数据库
Td>database
SQL terminology MongoDB terminology/Concepts explanations
database databases
table collection database Tables/ Collection
row document data record Line/document
columm Field numeric field/field
index index index
tabl E joins index table link MongoDB does not support
primary key primary key MongoDB automatically sets the--id field Primary key
    • Database
      一个MongoDB中可以建立多个数据库,MongoDB的默认库为test该数据库存储在data目录中。要显示它,需要向数据库插入一些数据。MongoDB的点个实例可以容纳多个独立的数据库,每一个都有自己的集合个权限,不同的数据库也放置在不同的文件中
    • Special database reserved by the database
      admin : 从权限的角度来看,这是root数据库。如果将一个用户添加到这个数据库,这个用户将自动继承所有数据库的权限。一些特定的服务器终端命令也只能从这个数据库运行,比如关闭。local:这个数据库永不会复制,可以用来存储限于本地单台服务器任意集合config:当Mongo用于分片设置时,config 数据库在内部使用,用于保存那些分片相关的信息
    • Collection
      集合就是MongonDB文档组,类似于关系型数据库管理系统(Relational DatabaseManagement)中的表格集合存在于数据库中,集合没有固定的结构,这意味着在结合中可以插入不同格式和类型的数据,但通常情况下插入集合的数据都会有一定的关联性。当第一个文档插入是,集合就会自动创建。
    • MongoDB Login, exit
      #启动服务mongod -f 对应的主配置文件#停止服务mongod -f 对应的主配置文件  ----shutdown#本地登录(默认实例端口号为:--port=27017,可以不写)> mongo#登录远程主机的实例> mongo --host 192.168.10.5 --port =27017#退出MongoDB> exit
    • Collection
创建info集合> db.createcollection(‘ccc‘)#查看集合方法一:> show tabels方法二:> show colletctions#显示info集合操作命令> db.info.help()
    • Document additions and deletions change
#插入一条记录> db.info.insert({"id":1,"name":"zzz","address":"机场","hobby":["game","talk","sport"]})#向指定集合中插入一条文档数据> db.collection.insertOne()#向指定集合中插入多条文档数据> db.collection.insertMany()#通过循环批量插入数据> for(var i=1;i<100;i++)db.info.insert({"id":i,"name":"jack"+i})删除 #删除info集合中id=1的文档> db.info.remove({"id":"1"}) 修改 #修改info集合id=1的name值为"zhangsan"文档db.info.update({"id":"1"},{$set:{"name":"zhangsan"}})查询#查询info集合所有文档> db.info.find()#查询info集合id为1的文档> db.info.findOne({id:1})#统计记录数> db.info.count()复制数据库> show dbs> db.copyDatabase("school","school_1") //复制一份一样的内容的数据库
    • Clone collection
克隆集合#登录端口号为27018的实例> mongo --port 27018#查询数据库> show dbsadmin   0.000GBconfig  0.000GBlocal   0.000GB#克隆端口号为27017实例的school数据库的info表至本实例数据库中> db.runCommand({"cloneCollection":"ccc.info","from":"192.168.100.5:27017"})用户认证#登录mongodbmongo
    • User Authorization (Authentication login)
#在admin数据库创建新用户root:123123> use admin> db.createUser({"user":"root","pwd":"123123","roles":["root"]})Successfully added user: { "user" : "root", "roles" : [ "root" ] }#退出> exit#关闭mongodb服务mongod -f /data/conf/mongodb1.conf --shutdown#带认证参数方式启动mongodb服务mongod -f /data/conf/mongodb1.conf --auth#登录mongodb数据库mongo#查询数据库show dbs> 不显示内容,这里要先授权认证后才能执行操作> use admin#使用授权root用户验证> db.auth("root":"123123")#再次查询,已经可以查询数据> show dbsadmin   0.000GBconfig  0.000GBccc     0.000GBschool  0.000GB
    • Backing up and recovering a database
      1. Export import data via mongoexport and Mongoimport directories;
      2. The format of the exported data file is: JSON format or CSV format;
        Parameter description:
    • D: Name of the database
    • Name of the C:collection
    • F: Which columns to export
    • O: The file name to export
    • Q: Filter criteria to export data
#备份本地school数据库> [[email protected] ~]# mkdir /backup  //创建备份目录[[email protected] ~]# mongodump -d ccc -o /backup/  //导出#恢复本地school数据库至数据库abc中> [[email protected] ~]# mongorestore -d abc --dir=/backup/school#导出本机school数据库info集合> [[email protected] ~]# mongoexport -d school -c info -o /backup/info.json#导入备份数据至本机school数据库user集合> [[email protected] ~]# mongoimport -d school -c user --file /backup/info.json#导出本机school数据库user1集合id=10的数据> [[email protected] ~]# mongoexport -d school -c user -q ‘{"id":{"$lt":10}}‘ -o /backup/top10.json

MongoDB Basic Operations 1 additions and deletions to check the article

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.