1. 进入mongo环境

 mongo

 [root@slave2 ~]# mongo

MongoDB shell version v3.4.16
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.16
Server has startup warnings: 
2018-07-20T17:53:38.109+0800 I CONTROL  [initandlisten] 
2018-07-20T17:53:38.109+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-07-20T17:53:38.109+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-07-20T17:53:38.109+0800 I CONTROL  [initandlisten] 
2018-07-20T17:53:38.109+0800 I CONTROL  [initandlisten] 
2018-07-20T17:53:38.109+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is ‘always’.
2018-07-20T17:53:38.109+0800 I CONTROL  [initandlisten] **        We suggest setting it to ‘never’
2018-07-20T17:53:38.109+0800 I CONTROL  [initandlisten] 
2018-07-20T17:53:38.109+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is ‘always’.
2018-07-20T17:53:38.109+0800 I CONTROL  [initandlisten] **        We suggest setting it to ‘never’
2018-07-20T17:53:38.109+0800 I CONTROL  [initandlisten] 

>

2.查询所有数据库

show dbs;   

 

 

> show dbs

 

admin  0.000GB
local  0.000GB
3.创建或者切换库(如果数据库不存在,则创建数据库,否则切换到指定数据库

use mydb; 

> use mydb
switched to db mydb
> show dbs
admin  0.000GB
local  0.000GB

可以看到,我们刚创建的数据库 runoob 并不在数据库的列表中, 要显示它,我们需要向 runoob 数据库插入一些数据。

4.创建集合 

> db.createCollection(“log”)
{ “ok” : 1 }
> show dbs
admin  0.000GB
local  0.000GB
mydb   0.000GB

5. 插入数据 (如果插入时集合不存在,则包括创建集合和插入数据两个动作)及更新

> db.logdata.insert({name:’zhangsan’, age:’25’});
WriteResult({ “nInserted” : 1 } 
> db.logdata.insert({‘_id’: ‘terrywang’, ‘super_admin’: true})

 

WriteResult({ “nInserted” : 1 }) 
> db.logdata.find({‘_id’:’terrywang’})

{ “_id” : “terrywang”, “super_admin” : true } 

> db.logdata.update({‘_id’: ‘terrywang’}, {$set: {‘super_admin’: false}})
WriteResult({ “nMatched” : 1, “nUpserted” : 0, “nModified” : 1 })
> db.logdata.find({“_id”:”terrywang”})
{ “_id” : “terrywang”, “super_admin” : false } 

6.查看集合内容

> db.logdata.find()
{ “_id” : ObjectId(“5b529ef8bfde59a50f5e5397”), “name” : “lisi”, “age” : “30” }
{ “_id” : “terrywang”, “super_admin” : false }
> db.logdata.findOne({‘_id’:’terrywang’})

{ “_id” : “terrywang”, “super_admin” : true }

 

7.按条件筛选

> db.logdata.find({“name”:”zhangsan”});
{ “_id” : ObjectId(“5b529e14bfde59a50f5e5396”), “name” : “zhangsan”, “age” : “25” }

正则查找 

> db.logdata.find({“_id”:/e*/i})
{ “_id” : “terrywang”, “super_admin” : false }

   i表明是否是case-insensitive,有i则表示忽略大小写 

只筛选到某一列 

> db.logdata.find({},{D:1})

{ “_id” : ObjectId(“5b529ad3aea13d710cc622d5”) } 

 8.删除文档

先插入一个文档 

> db.logdata.insert({name:’lisi’, age:’30’});
WriteResult({ “nInserted” : 1 })
> db.logdata.find()
{ “_id” : ObjectId(“5b529e14bfde59a50f5e5396”), “name” : “zhangsan”, “age” : “25” }
{ “_id” : ObjectId(“5b529ef8bfde59a50f5e5397”), “name” : “lisi”, “age” : “30” } 

删除一个文档

> db.logdata.remove({“name”:”zhangsan”})

 

WriteResult({ “nRemoved” : 1 })
> db.logdata.find()
{ “_id” : ObjectId(“5b529ef8bfde59a50f5e5397”), “name” : “lisi”, “age” : “30” }

 9.显示集合 

 

 

> show tables;

 

log
logdata

10.显示集合中文档数

> db.logdata.find()
{ “_id” : ObjectId(“5b529ef8bfde59a50f5e5397”), “name” : “lisi”, “age” : “30” }
{ “_id” : “terrywang”, “super_admin” : false }
> db.logdata.count()
2

11.查看集合索引

> db.logdata.getIndex()
2018-07-21T11:42:09.337+0800 E QUERY    [thread1] TypeError: db.logdata.getIndex is not a function :
@(shell):1:1
> db.logdata.getIndexes()
[
        {
                “v” : 2,
                “key” : {
                        “_id” : 1
                },
                “name” : “_id_”,
                “ns” : “mydb.logdata”
        }

12.删除表 

> db.logdata.drop()

 

true
> show tables;
log
test
> db.test.drop()
true
> show tables
log

 

posted on 2018-07-21 11:51 王晓成 阅读() 评论() 编辑 收藏
版权声明:本文为abcdwxc原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/abcdwxc/p/9346142.html