MongoDB Basic Command Miscellaneous

Source: Internet
Author: User
Tags install mongodb yum install mongodb

One, MongoDB installation use 1, installation start MongoDB1.1 download installation
官网:https://www.mongodb.com/rpm:http://mirrors.aliyun.com/mongodb/yum/redhat/7/mongodb-org/[[email protected] ~]# ls mongodb-3.0.0/mongodb-org-3.0.0-1.el7.x86_64.rpmmongodb-org-mongos-3.0.0-1.el7.x86_64.rpmmongodb-org-server-3.0.0-1.el7.x86_64.rpmmongodb-org-shell-3.0.0-1.el7.x86_64.rpmmongodb-org-tools-3.0.0-1.el7.x86_64.rpm暂时不需要分片功能(mongos),只安装这三个包:[[email protected] mongodb-3.0.0]# yum install mongodb-org-server-3.0.0-1.el7.x86_64.rpm mongodb-org-shell-3.0.0-1.el7.x86_64.rpm mongodb-org-tools-3.0.0-1.el7.x86_64.rpm 查看包组相关信息:[[email protected] ~]# rpm -qi mongodb-org-server查看包组生成的文件:[[email protected] ~]# rpm -ql mongodb-org-server
1.2 Configuration, starting
创建数据存放目录:[[email protected] ~]# mkdir -pv /mongodb/data[[email protected] ~]# chown -R mongod:mongod /mongodb/配置[[email protected] ~]# vim /etc/mongod.conf dbpath=/mongodb/data#注释掉监听地址后,默认监听在0.0.0.0地址上#bind_ip=127.0.0.1#开启WEB监听httpinterface=truerest=true启动服务:[[email protected] ~]# systemctl start mongod服务启动后初始化的数据文件:[[email protected] ~]# ls /mongodb/data/journal  local.0  local.ns  mongod.lock  storage.bson  _tmp

Access Web interface: http://10.201.106.131:28017/

1.3 Client connections
获取客户端命令使用帮助:[[email protected] ~]# mongo -h连接:[[email protected] ~]# mongo --host 10.201.106.131显示当前有多少个数据库:> show dbsMongoDB可以不创建数据库,直接使用(在库里面创建表后,库会自动创建出来,延时创建):> use testdbswitched to db testdb> show dbslocal  0.078GB
2, MongoDB Basic use 2.1 basic operation
获取帮助:> help显示用户(默认没有开启认证信息):> show users显示已存在的数据库> show dbs显示当前库的collections(名称列表)> show collections查看可以用在db级别的命令:> db.help()
2.2 DB Command
查看当前数据库信息:> db.stats()查看数据库版本号:> db.version()查看服务器级别状态信息> db.serverStatus()显示当前库内的所有名称列表:> db.getCollectionNames()
3. CRUD Statement 3.1 creation
创建collection并插入数据> db.students.insert({name:"tom",age:23})WriteResult({ "nInserted" : 1 })查看collection:> show collectionsstudentssystem.indexes同时自动创建了数据库:> show dbs;local   0.078GBtestdb  0.078GB查看collection状态信息:> db.students.stats()查看库里的collection:> db.getCollectionNames()[ "students", "system.indexes" ]继续在students插入数据:> db.students.insert({name:"jerry",age:40,gender:"M"})WriteResult({ "nInserted" : 1 })
3.2 Document (collection) operation
获取文档操作命令:> db.mycollect.help()查看一个命令的使用帮助:> db.mycollect.find().help()查看collection里面的文档:> db.students.find(){ "_id" : ObjectId("587bc5e2a954a5d5a876fb24"), "name" : "tom", "age" : 23 }{ "_id" : ObjectId("587bd2f0a954a5d5a876fb25"), "name" : "jerry", "age" : 40, "gender" : "M" }统计collection里面的文档个数> db.students.count()2多插入几个数据> db.students.insert({name:"Ou Yangfeng",Age:90,Course:"HaMegong"})WriteResult({ "nInserted" : 1 })> db.students.insert({name:"Yang Guo",Age:20,Course:"Meinv Quan"})WriteResult({ "nInserted" : 1 })> db.students.insert({name:"Guo Jing",Age:40,Course:"Xianglong Shibazhang"})WriteResult({ "nInserted" : 1 })
3.3 Query (Find)
Query for records older than 30:> db.students.find ({age: {$gt: +}}) {"_id": ObjectId ("587bd99ea954a5d5a876fb26"), "name": "Ou Yangfeng "Age": "Course": "Hamegong"} {"_id": ObjectId ("587bd9f4a954a5d5a876fb28"), "name": "Guo Jing", "age": +, "Co Urse ":" Xianglong Shibazhang "} Find records of age 20 and 40:> db.students.find ({age:{$in: [20,40]}}) {" _id ": ObjectId (" 587bd9bfa954a5d5a876fb27 ")," name ":" Yang Guo "," age ": +," Course ":" Meinv Quan "} {" _id ": ObjectId (" 587bd9f4a954a5d5 A876fb28 ")," name ":" Guo Jing "," age ": +," Course ":" Xianglong Shibazhang "} Look for records that are older than 20 and 40:> Db.students.find ({ages: {$nin: [20,40]}}) $or: OR operation, syntax format {$or: [{<expression1>},{...}]} $and: With Operation $not: Non-op $nor: Inverse operation, returns all documents that do not match the specified criteria find age=20,40 or age=20,40 records > Db.students.find ({$or: [{age:{$in: [20,40]}}, {age:{$in: [20,40]}}]}) {"_id": ObjectId ("587bd2f0a954a5d5a876fb25"), "name": "Jerry", "age": +, "gender": "M"} {"_id": ObjectId ("587bd9bfa954a5d5a876fb27"), "name": "Yang Guo", "age": +, "Course": "Meinv Quan"} { "_ID ": ObjectId (" 587bd9f4a954a5d5a876fb28 ")," name ":" Guo Jing "," age ": +," Course ":" Xianglong Shibazhang "} View gender word section of the document:> Db.students.find ({gender: {$exists: true}}) {"_id": ObjectId ("587bd2f0a954a5d5a876fb25"), "name": "Jerry", " Age ": +," gender ":" M "}
3.4 Update
文档操作帮助:> db.mycoll.help()下面命令默认只更新一个文档(一行),如需更新整个collection要加multi_bool参数db.mycoll.update()修改tom的年龄为21:> db.students.update({name:"tom"},{$set: {age: 21}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.students.find({name:"tom"}){ "_id" : ObjectId("587bc5e2a954a5d5a876fb24"), "name" : "tom", "age" : 21 }修改age字段名为Age:原始数据:> db.students.find({age: {$exists: true}}){ "_id" : ObjectId("587bc5e2a954a5d5a876fb24"), "name" : "tom", "age" : 21 }{ "_id" : ObjectId("587bd2f0a954a5d5a876fb25"), "name" : "jerry", "age" : 40, "gender" : "M" }修改:> db.students.update({},{$rename: {"age": "Age"}},false,true)WriteResult({ "nMatched" : 5, "nUpserted" : 0, "nModified" : 2 })
3.5 Delete
删除年龄为21的文档(行):> db.students.remove({Age: 21})删除collection(类似删除整张表):> db.teachers.drop()true删除当前数据库:> db.dropDatabase()> show dbslocal  0.078GB
3.6 Other
Statistics Query number:> db.students.find ({age: {$in: [20,40]}}) {"_id": ObjectId ("587bd2f0a954a5d5a876fb25"), "name": "Jerry", " Gender ":" M "," age ": + {" _id ": ObjectId (" 587bd9bfa954a5d5a876fb27 ")," name ":" Yang Guo "," Course ":" Meinv Quan ","  Age ":} {" _id ": ObjectId (" 587bd9f4a954a5d5a876fb28 ")," name ":" Guo Jing "," Course ":" Xianglong Shibazhang "," Age ": }> Db.students.find ({age: {$in: [20,40]}}). Count () 3 displays only one line > Db.students.find ({age: {$in: [20,40]}}). Limit (1) {" _id ": ObjectId (" 587bd2f0a954a5d5a876fb25 ")," name ":" Jerry "," Gender ":" M "," Age ": 40} skips the first line, and then shows the remaining rows:> db.students.f IND ({age: {$in: [20,40]}}). Skip (1) {"_id": ObjectId ("587bd9bfa954a5d5a876fb27"), "name": "Yang Guo", "Course": "Meinv Q Uan "," Age ":} {" _id ": ObjectId (" 587bd9f4a954a5d5a876fb28 ")," name ":" Guo Jing "," Course ":" Xianglong Shibazhang ", "Age": 40} Only one of the:> Db.students.findOne ({age: {$gt: Ten}}) {"_id": ObjectId ("587bd2f0a954a5d5a876fb25") is displayed when the condition is met, " Name ":" Jerry "," Gender ":"M", "Age": 40} 

MongoDB Basic Command notes

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.