標籤:io sp strong on 資料 bs cti new nbsp
/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
Mongodb shell 基本操作