查看全部資料表
> use ChatRoom
switched to db ChatRoom
> show collections
Account
Chat
system.indexes
system.users
建立資料表
> db.createCollection("Account")
{"ok":1}
> db.createCollection("Test",{capped:true, size:10000}) { "ok" : 1 }
{"ok":1}
-- 說明
capped:true,表示該集合的結構不能被修改;
size:在建表之初就指定一定的空間大小,接下來的插入操作會不斷地按順序APPEND資料在這個預分配好空間的檔案中,如果已經超出空間大小,則回到檔案頭覆蓋原來的資料繼續插入。這種結構保證了插入和查詢的高效性,它不允許刪除單個記錄,更新的也有限制:不能超過原有記錄的大小。這種表效率很高,它適用於一些暫時儲存資料的場合,比如網站中登入使用者的session資訊,又比如一些程式的監控日誌,都是屬於過了一定的時間就可以被覆蓋的資料。
修改資料表名
> db.Account.renameCollection("Account1")
{ "ok" : 1 }
資料表說明主題help
> db.Account.help()
DBCollection help
db.Account.find().help() - show DBCursor help
db.Account.count()
db.Account.dataSize()
db.Account.distinct( key ) - eg. db.Account.distinct( 'x' )
db.Account.drop() drop the collection
db.Account.dropIndex(name)
db.Account.dropIndexes()
db.Account.ensureIndex(keypattern[,options]) - options is an object with these possible fields: name, unique, dropDups
db.Account.reIndex()
db.Account.find([query],[fields]) - query is an optional query filter. fields is optional set of fields to return.
e.g. db.Account.find( {x:77} , {name:1, x:1} )
db.Account.find(...).count()
db.Account.find(...).limit(n)
db.Account.find(...).skip(n)
db.Account.find(...).sort(...)
db.Account.findOne([query])
db.Account.findAndModify( { update : ... , remove : bool [, query: {}, sort: {}, 'new': false] } )
db.Account.getDB() get DB object associated with collection
db.Account.getIndexes()
db.Account.group( { key : ..., initial: ..., reduce : ...[, cond: ...] } )
db.Account.mapReduce( mapFunction , reduceFunction , <optional params> )
db.Account.remove(query)
db.Account.renameCollection( newName , <dropTarget> ) renames the collection.
db.Account.runCommand( name , <options> ) runs a db command with the given name where the first param is the collection name
db.Account.save(obj)
db.Account.stats()
db.Account.storageSize() - includes free space allocated to this collection
db.Account.totalIndexSize() - size in bytes of all the indexes
db.Account.totalSize() - storage allocated for all data and indexes
db.Account.update(query, object[, upsert_bool, multi_bool])
db.Account.validate() - SLOW
db.Account.getShardVersion() - only for use with sharding
查看全部表記錄
> db.Account.find()
{ "_id" : ObjectId("4df08553188e444d001a763a"), "AccountID" : 1, "UserName" : "libing", "Password" : "1", "Age" : 26, "Email" : "libing@126.com", "RegisterDate" : "2011-06-09 16:31:25" }
{ "_id" : ObjectId("4df08586188e444d001a763b"), "AccountID" : 2, "UserName" : "lb", "Password" : "1", "Age" : 25, "Email" : "libing@163.com", "RegisterDate" : "2011-06-09 16:36:95" }
--SELECT * FROM Account
說明:
預設每頁顯示20條記錄,當顯示不下的情況下,可以用it迭代命令查詢下一頁資料。
可以通過DBQuery.shellBatchSize設定每頁顯示資料的大小。如:DBQuery.shellBatchSize = 5,這樣每頁就顯示5條記錄了。
> db.Test.find()
{ "_id" : ObjectId("4df6d55407444568af61cfea"), "TestID" : 1 }
{ "_id" : ObjectId("4df6d55907444568af61cfeb"), "TestID" : 2 }
{ "_id" : ObjectId("4df6d55b07444568af61cfec"), "TestID" : 3 }
{ "_id" : ObjectId("4df6d55e07444568af61cfed"), "TestID" : 4 }
{ "_id" : ObjectId("4df6d56207444568af61cfee"), "TestID" : 5 }
{ "_id" : ObjectId("4df6d56507444568af61cfef"), "TestID" : 6 }
{ "_id" : ObjectId("4df6d56807444568af61cff0"), "TestID" : 7 }
{ "_id" : ObjectId("4df6d56b07444568af61cff1"), "TestID" : 8 }
{ "_id" : ObjectId("4df6d56e07444568af61cff2"), "TestID" : 9 }
{ "_id" : ObjectId("4df6d57a07444568af61cff3"), "TestID" : 10 }
{ "_id" : ObjectId("4df6d57d07444568af61cff4"), "TestID" : 11 }
{ "_id" : ObjectId("4df6d58007444568af61cff5"), "TestID" : 12 }
{ "_id" : ObjectId("4df6d58307444568af61cff6"), "TestID" : 13 }
{ "_id" : ObjectId("4df6d58e07444568af61cff7"), "TestID" : 14 }
{ "_id" : ObjectId("4df6d59207444568af61cff8"), "TestID" : 15 }
{ "_id" : ObjectId("4df6d59607444568af61cff9"), "TestID" : 16 }
{ "_id" : ObjectId("4df6d59c07444568af61cffa"), "TestID" : 17 }
{ "_id" : ObjectId("4df6d5a307444568af61cffb"), "TestID" : 18 }
{ "_id" : ObjectId("4df6d5a607444568af61cffc"), "TestID" : 19 }
> DBQuery.shellBatchSize
20
> DBQuery.shellBatchSize = 5
5
> db.Test.find()
{ "_id" : ObjectId("4df6d55407444568af61cfea"), "TestID" : 1 }
{ "_id" : ObjectId("4df6d55907444568af61cfeb"), "TestID" : 2 }
{ "_id" : ObjectId("4df6d55b07444568af61cfec"), "TestID" : 3 }
{ "_id" : ObjectId("4df6d55e07444568af61cfed"), "TestID" : 4 }
{ "_id" : ObjectId("4df6d56207444568af61cfee"), "TestID" : 5 }
has more
> it
{ "_id" : ObjectId("4df6d56507444568af61cfef"), "TestID" : 6 }
{ "_id" : ObjectId("4df6d56807444568af61cff0"), "TestID" : 7 }
{ "_id" : ObjectId("4df6d56b07444568af61cff1"), "TestID" : 8 }
{ "_id" : ObjectId("4df6d56e07444568af61cff2"), "TestID" : 9 }
{ "_id" : ObjectId("4df6d57a07444568af61cff3"), "TestID" : 10 }
has more
> it
{ "_id" : ObjectId("4df6d57d07444568af61cff4"), "TestID" : 11 }
{ "_id" : ObjectId("4df6d58007444568af61cff5"), "TestID" : 12 }
{ "_id" : ObjectId("4df6d58307444568af61cff6"), "TestID" : 13 }
{ "_id" : ObjectId("4df6d58e07444568af61cff7"), "TestID" : 14 }
{ "_id" : ObjectId("4df6d59207444568af61cff8"), "TestID" : 15 }
has more
> it
{ "_id" : ObjectId("4df6d59607444568af61cff9"), "TestID" : 16 }
{ "_id" : ObjectId("4df6d59c07444568af61cffa"), "TestID" : 17 }
{ "_id" : ObjectId("4df6d5a307444568af61cffb"), "TestID" : 18 }
{ "_id" : ObjectId("4df6d5a607444568af61cffc"), "TestID" : 19 }
> it
no cursor
查詢一條記錄
> db.Account.findOne()
{
"_id" : ObjectId("4ded95c3b7780a774a099b7c"),
"UserName" : "libing",
"Password" : "1",
"Email" : "libing@126.cn",
"RegisterDate" : "2011-06-07 11:06:25"
}
--SELECT TOP 1 * FROM Account
查詢聚集中欄位的不同記錄
> db.Account.distinct("UserName")
--SELECT DISTINCT("UserName") FROM Account
查詢聚集中UserName包含“keyword”關鍵字的記錄
db.Account.find({"UserName":/keyword/})
--SELECT * FROM Account WHERE UserName LIKE '%keyword%'
查詢聚集中UserName以"keyword" 開頭的記錄
> db.Account.find({"UserName":/^keyword/})
--SELECT * FROM Account WHERE UserName LIKE 'keyword%'
查詢聚集中UserName以“keyword”結尾的記錄
> db.Account.find({"UserName":/keyword$/})
--SELECT * FROM Account WHERE UserName LIKE '%keyword'
查詢聚集中指定列
> db.Account.find({},{"UserName":1,"Email":1}) --1:true
--SELECT UserName,Email FROM Account
查詢聚集中排除指定列
> db.Account.find({},{"UserName":0}) --0:false
查詢聚集中指定列,且Age > 20
> db.Account.find({"Age":{"$gt":20}},{"UserName":1,"Email":1})
--SELECT UserName,Email FROM Account WHERE Age > 20
聚集中欄位排序
> db.Account.find().sort({"UserName":1}) -- 升序
> db.Account.find().sort({"UserName":-1}) --降序
--SELECT * FROM Account ORDER BY UserName ASC
--SELECT * FROM Account ORDER BY UserName DESC
統計聚集中記錄條數
> db.Account.find().count()
--SELECT COUNT(*) FROM Account
統計聚集中合格記錄條數
> db.Account.find({"Age":{"$gt":20}}).count()
-- SELECT COUNT(*) FROM Account WHERE Age > 20
統計聚集中欄位合格記錄條數
> db.Account.find({"UserName":{"$exists":true}}).count()
--SELECT COUNT(UserName) FROM Account
查詢聚集中前5條記錄
> db.Account.find().limit(5)
--SELECT TOP 5 * FROM Account
查詢聚集中第10條以後的記錄
> db.Account.find().skip(10)
--SELECT * FROM Account WHERE AccountID NOT IN (SELECT TOP 10 AccountID FROM Account)
查詢聚集中第10條記錄以後的5條記錄
> db.Account.find().skip(10).limit(5)
--SELECT TOP 5 * FROM Account WHERE AccountID NOT IN (SELECT TOP 10 AccountID FROM Account)
or查詢
> db.Account.find({"$or":[{"UserName":/keyword/},{"Email":/keyword/}]},{"UserName":true,"Email":true})
--SELECT UserName,Email FROM Account WHERE UserName LIKE '%keyword%' OR Email LIKE '%keyword%'
添加新記錄
> db.Account.insert({AccountID:2,UserName:"lb",Password:"1",Age:25,Email:"libing@163.com",RegisterDate:"2011-06-09 16:36:95"})
修改記錄
> db.Account.update({"AccountID":1},{"$set":{"Age":27,"Email":"libingql@163.com"}})
> db.Account.find({"AccountID":1})
{ "AccountID" : 1, "Age" : 27, "Email" : "libingql@163.com", "Password" : "1", "RegisterDate" : "2011-06-09 16:31:25", "UserName" : "libing", "_id" : ObjectId("4df08553188e444d001a763a") }
> db.Account.update({"AccountID":1},{"$inc":{"Age":1}})
> db.Account.find({"AccountID":1})
{ "AccountID" : 1, "Age" : 28, "Email" : "libingql@163.com", "Password" : "1", "RegisterDate" : "2011-06-09 16:31:25", "UserName" : "libing", "_id" : ObjectId("4df08553188e444d001a763a") }
刪除記錄
> db.Account.remove({"AccountID":1}) --DELETE FROM Account WHERE AccountID = 1
> db.Account.remove({"UserName":"libing"}) --DELETE FROM Account WHERE UserName = 'libing'
> db.Account.remove({"Age":{$lt:20}}) --DELETE FROM Account WHERE Age < 20
> db.Account.remove({"Age":{$lte:20}}) --DELETE FROM Account WHERE Age <= 20
> db.Account.remove({"Age":{$gt:20}}) --DELETE FROM Account WHERE Age > 20
> db.Account.remove({"Age":{$gte:20}}) --DELETE FROM Account WHERE Age >= 20
> db.Account.remove({"Age":{$ne:20}}) --DELETE FROM Account WHERE Age != 20
> db.Account.remove() --全部刪除
> db.Account.remove({}) --全部刪除