mongoDB基本使用(二),mongodb使用
資料庫基本操作串連到mongoDB伺服器 ./bin/mongo 127.0.0.1:12345
查看當前資料庫> show dbsadmin (empty)local 0.078G
卻換資料庫(如果不存在會自動建立)> use jeromeswitched to db jerome
刪除資料庫> db.dropDatabase(){ "dropped" : "jerome", "ok" : 1 }
刪除表
12345678910 |
> > show tables jerome_collection jerome_coolection system.indexes > db.jerome_collection.drop() true > show tables #刪除了當前表了 jerome_coolection system.indexes > |
寫入(集合資料的寫入,格式為JSON)> db.jerome_collection.insert({x:1})WriteResult({ "nInserted" : 1 })(插入成功)
查詢> show dbsadmin (empty)jerome 0.078GBlocal 0.078GB> show collectionsjerome_collectionsystem.indexes> db.jerome_collection.find(){ "_id" : ObjectId("556fd29a4e8b96c5ebc42e63"), "x" : 1 }> db.jerome_collection.find({x:1}) #可以指定參數{ "_id" : ObjectId("556fd29a4e8b96c5ebc42e63"), "x" : 1 }
(_id是全域欄位,在資料庫中不會重複)
測試:再插入一條資料_id為1
1234567891011 |
> db.jerome_collection.insert({x:3,_id:1}) WriteResult({ "nInserted" : 1 }) > db.jerome_collection.insert({x:2,_id:1}) WriteResult({ "nInserted" : 0, "writeError" : { "code" : 11000, "errmsg" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: jerome.jerome_collection.$_id_ dup key: { : 1.0 }" } }) > |
插入兩次一樣的id會報錯,id不可以重複。
插入多條資料測試limit等
12345678 |
> for (i=3;i<100;i++)db.jerome_collection.insert({x:i}) #可以使用js文法 WriteResult({ "nInserted" : 1 }) > db.jerome_collection. find ().count() #尋找總條數 99 > db.jerome_collection. find ().skip(3).limit(2). sort ({x:1}) #跳過前三條,取兩條,使用x排序 { "_id" : ObjectId( "556ff5e8d7e60a53de941a74" ), "x" : 4 } { "_id" : ObjectId( "556ff5e8d7e60a53de941a75" ), "x" : 5 } > |
更新Updata至少接收兩個參數,一個尋找的,一個更新的資料。
12345678 |
> db.jerome_collection. find ({x:1}) { "_id" : ObjectId( "556fd29a4e8b96c5ebc42e63" ), "x" : 1 } > db.jerome_collection.update({x:1},{x:999}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.jerome_collection. find ({x:1}) #已經找不到了 > db.jerome_collection. find ({x:999}) { "_id" : ObjectId( "556fd29a4e8b96c5ebc42e63" ), "x" : 999 } > |
部分更新操作符(set )
1234567 |
> db.jerome_collection.insert({x:100,y:100,z:100}) WriteResult({ "nInserted" : 1 }) > db.jerome_collection.update({z:100},{$ set :{y:99}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.jerome_collection. find ({z:100}) { "_id" : ObjectId( "556ff84a1c99195ded71252e" ), "x" : 100, "y" : 99, "z" : 100 } > |
更新不存在資料時會自動建立
12345678910 |
> db.jerome_collection. find ({y:100}) > db.jerome_collection.update({y:100},{y:999}, true ) WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : ObjectId( "556ff9556db7cf8009b5edf8" ) }) > db.jerome_collection. find ({y:999}) { "_id" : ObjectId( "556ff9556db7cf8009b5edf8" ), "y" : 999 } |
更新多條資料
123456789101112131415161718192021 |
> for (i=0;i<3;i++)db.jerome_collection.insert({c:2}) #插入三條 WriteResult({ "nInserted" : 1 }) > db.jerome_collection. find ({c:2}) { "_id" : ObjectId( "556ffa011c99195ded71252f" ), "c" : 2 } { "_id" : ObjectId( "556ffa011c99195ded712530" ), "c" : 2 } { "_id" : ObjectId( "556ffa011c99195ded712531" ), "c" : 2 } > db.jerome_collection.update({c:2},{c:3}) #更新 WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.jerome_collection. find ({c:2}) { "_id" : ObjectId( "556ffa011c99195ded712530" ), "c" : 2 } { "_id" : ObjectId( "556ffa011c99195ded712531" ), "c" : 2 } > db.jerome_collection. find ({c:3}) #發現只更新一條,是為了防止誤操作 { "_id" : ObjectId( "556ffa011c99195ded71252f" ), "c" : 3 } > db.jerome_collection.update({c:2},{$ set :{c:3}}, false , true ) #更新多條 WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 }) > db.jerome_collection. find ({c:2}) > db.jerome_collection. find ({c:3}) { "_id" : ObjectId( "556ffa011c99195ded71252f" ), "c" : 3 } { "_id" : ObjectId( "556ffa011c99195ded712530" ), "c" : 3 } { "_id" : ObjectId( "556ffa011c99195ded712531" ), "c" : 3 } |
刪除(必須要有參數)
1234567891011121314 |
> db.jerome_collection. find ({c:3}) { "_id" : ObjectId( "556ffa011c99195ded71252f" ), "c" : 3 } { "_id" : ObjectId( "556ffa011c99195ded712530" ), "c" : 3 } { "_id" : ObjectId( "556ffa011c99195ded712531" ), "c" : 3 } > db.jerome_collection.remove() #不可用 2015-06-04T00:15:34.444-0700 remove needs a query at src /mongo/shell/collection .js:299 > db.jerome_collection. find ({c:3}) { "_id" : ObjectId( "556ffa011c99195ded71252f" ), "c" : 3 } { "_id" : ObjectId( "556ffa011c99195ded712530" ), "c" : 3 } { "_id" : ObjectId( "556ffa011c99195ded712531" ), "c" : 3 } > db.jerome_collection.remove({c:3}) #刪除必須要有參數 WriteResult({ "nRemoved" : 3 }) > db.jerome_collection. find ({c:3}) #刪除成功 > |
索引資料較多時,使用索引速度加快。查看集合索引情況
1234567891011121314 |
> for (i=0;i<100;i++)db.jerome_collection.insert({x:i}) #添加測試資料 WriteResult({ "nInserted" : 1 }) > db.jerome_collection.getIndexes() [ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_" , "ns" : "jerome.jerome_collection" } ] > |
只有一個預設索引。
建立索引1代表正向排序,-1代表反向排序
123456789101112131415161718192021222324252627 |
> db.jerome_collection.ensureIndex({x:1}) { "createdCollectionAutomatically" : false , "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.jerome_collection.getIndexes() [ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_" , "ns" : "jerome.jerome_collection" }, { "v" : 1, "key" : { "x" : 1 }, "name" : "x_1" , "ns" : "jerome.jerome_collection" } ] > |
(使用資料庫之前建立索引更好)
索引雖然會使寫入的數度變慢,但是查詢的速度變快了。