Mongodb shell 基本操作

时间:2021-08-02 02:36:39


/opt/mongodb-2.6.6/bin > mongo

 

1. 查询本地所有数据库名称
> show dbs

 

2. 切换至指定数据库环境(若无指定的数据库,则创建新的库)
> use test;

 

3. 创建了一个名为'emp'的集合
> db.createCollection('emp');
{ "ok" : 1 }

 

4. 查询当前库下的所有聚集集合collection(相当于table)
> show collections;
emp
system.indexes

 

5. 插入记录
> db.emp.insert({'id':1, 'name':'Tough01', 'age':27});
WriteResult({ "nInserted" : 1 })

db.emp.insert({'id':1, 'name':'Tough1', 'age':21});
db.emp.insert({'id':2, 'name':'Tough2', 'age':22});
db.emp.insert({'id':3, 'name':'Tough3', 'age':23});
db.emp.insert({'id':4, 'name':'Tough4', 'age':24});
db.emp.insert({'id':5, 'name':'Tough5', 'age':25});
db.emp.insert({'id':6, 'name':'Tough6', 'age':26});
db.emp.insert({'id':7, 'name':'Tough7', 'age':27});
db.emp.insert({'id':8, 'name':'Tough8', 'age':28});
db.emp.insert({'id':9, 'name':'Tough9', 'age':29});

 

6. 查询操作
1) 查询emp的row_count
> db.emp.count();
9

2) 查询 id=6的记录
> db.emp.find({"id":6});
{ "_id" : ObjectId("548c0acbd6ce26c709fee232"), "id" : 6, "name" : "Tough6", "age" : 26 }

3) 查询 id<3 or age>27 的记录
> db.emp.find({$or: [{id:{$lt:3}}, {age:{$gt:27}}]});
{ "_id" : ObjectId("548c0acbd6ce26c709fee22d"), "id" : 1, "name" : "Tough1", "age" : 21 }
{ "_id" : ObjectId("548c0acbd6ce26c709fee22e"), "id" : 2, "name" : "Tough2", "age" : 22 }
{ "_id" : ObjectId("548c0acbd6ce26c709fee234"), "id" : 8, "name" : "Tough8", "age" : 28 }
{ "_id" : ObjectId("548c0acbd6ce26c709fee235"), "id" : 9, "name" : "Tough9", "age" : 29 }

4) 查询 age,name列
> db.emp.find({id:{$lt:7}, age:{$gt:23}}, {age:1, name:1});
{ "_id" : ObjectId("548c0acbd6ce26c709fee230"), "name" : "Tough4", "age" : 24 }
{ "_id" : ObjectId("548c0acbd6ce26c709fee231"), "name" : "Tough5", "age" : 25 }
{ "_id" : ObjectId("548c0acbd6ce26c709fee232"), "name" : "Tough6", "age" : 26 }

5) 查询 name 里包含5的记录
> db.emp.find({name:/5/});
{ "_id" : ObjectId("548c0acbd6ce26c709fee231"), "id" : 5, "name" : "Tough5", "age" : 25 }

6) 查询前2条记录
> db.emp.find().limit(2);
{ "_id" : ObjectId("548c0acbd6ce26c709fee22d"), "id" : 1, "name" : "Tough1", "age" : 21 }
{ "_id" : ObjectId("548c0acbd6ce26c709fee22e"), "id" : 2, "name" : "Tough2", "age" : 22 }

7) 查询第一条记录
> db.emp.findOne();
{
"_id" : ObjectId("548c0acbd6ce26c709fee22d"),
"id" : 1,
"name" : "Tough1",
"age" : 21
}

8) 按age升序
> db.emp.find().sort({age:1});
{ "_id" : ObjectId("548c0acbd6ce26c709fee22d"), "id" : 1, "name" : "Tough1", "age" : 21 }
{ "_id" : ObjectId("548c0acbd6ce26c709fee22e"), "id" : 2, "name" : "Tough2", "age" : 22 }
{ "_id" : ObjectId("548c0acbd6ce26c709fee22f"), "id" : 3, "name" : "Tough3", "age" : 23 }
{ "_id" : ObjectId("548c0acbd6ce26c709fee230"), "id" : 4, "name" : "Tough4", "age" : 24 }
{ "_id" : ObjectId("548c0acbd6ce26c709fee231"), "id" : 5, "name" : "Tough5", "age" : 25 }
{ "_id" : ObjectId("548c0acbd6ce26c709fee232"), "id" : 6, "name" : "Tough6", "age" : 26 }
{ "_id" : ObjectId("548c0acbd6ce26c709fee233"), "id" : 7, "name" : "Tough7", "age" : 27 }
{ "_id" : ObjectId("548c0acbd6ce26c709fee234"), "id" : 8, "name" : "Tough8", "age" : 28 }
{ "_id" : ObjectId("548c0acbd6ce26c709fee235"), "id" : 9, "name" : "Tough9", "age" : 29 }

9) 按age降序
> db.emp.find().sort({age:-1});
{ "_id" : ObjectId("548c0acbd6ce26c709fee235"), "id" : 9, "name" : "Tough9", "age" : 29 }
{ "_id" : ObjectId("548c0acbd6ce26c709fee234"), "id" : 8, "name" : "Tough8", "age" : 28 }
{ "_id" : ObjectId("548c0acbd6ce26c709fee233"), "id" : 7, "name" : "Tough7", "age" : 27 }
{ "_id" : ObjectId("548c0acbd6ce26c709fee232"), "id" : 6, "name" : "Tough6", "age" : 26 }
{ "_id" : ObjectId("548c0acbd6ce26c709fee231"), "id" : 5, "name" : "Tough5", "age" : 25 }
{ "_id" : ObjectId("548c0acbd6ce26c709fee230"), "id" : 4, "name" : "Tough4", "age" : 24 }
{ "_id" : ObjectId("548c0acbd6ce26c709fee22f"), "id" : 3, "name" : "Tough3", "age" : 23 }
{ "_id" : ObjectId("548c0acbd6ce26c709fee22e"), "id" : 2, "name" : "Tough2", "age" : 22 }
{ "_id" : ObjectId("548c0acbd6ce26c709fee22d"), "id" : 1, "name" : "Tough1", "age" : 21 }


7. 修改操作
修改id=1记录的name为'Fox'。
> db.emp.update({id:1},{$set:{name:'Fox'}}, false, true);
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

> db.emp.find({id:1});
{ "_id" : ObjectId("548c0acbd6ce26c709fee22d"), "id" : 1, "name" : "Fox", "age" : 21 }


db.collection.update( criteria, objNew, upsert, multi )
criteria : update的查询条件,类似sql update查询内where后面的
objNew : update的对象和一些更新的操作符(如$,$inc...)等,也可以理解为sql update查询内set后面的
upsert : 如果不存在update的记录,是否插入objNew,true为插入,默认是false,不插入。
multi : mongodb默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。

 

8. 删除操作
删除 id=9的记录。
> db.emp.remove({id:9});
WriteResult({ "nRemoved" : 1 })

> db.emp.find();
{ "_id" : ObjectId("548c0acbd6ce26c709fee22d"), "id" : 1, "name" : "Fox", "age" : 21 }
{ "_id" : ObjectId("548c0acbd6ce26c709fee22e"), "id" : 2, "name" : "Tough2", "age" : 22 }
{ "_id" : ObjectId("548c0acbd6ce26c709fee22f"), "id" : 3, "name" : "Tough3", "age" : 23 }
{ "_id" : ObjectId("548c0acbd6ce26c709fee230"), "id" : 4, "name" : "Tough4", "age" : 24 }
{ "_id" : ObjectId("548c0acbd6ce26c709fee231"), "id" : 5, "name" : "Tough5", "age" : 25 }
{ "_id" : ObjectId("548c0acbd6ce26c709fee232"), "id" : 6, "name" : "Tough6", "age" : 26 }
{ "_id" : ObjectId("548c0acbd6ce26c709fee233"), "id" : 7, "name" : "Tough7", "age" : 27 }
{ "_id" : ObjectId("548c0acbd6ce26c709fee234"), "id" : 8, "name" : "Tough8", "age" : 28 }


9. 重命名集合
> db.emp.renameCollection('test_emp');
{ "ok" : 1 }

 

10. 删除集合
> db.test_emp.drop();
true

> show collections;
system.indexes