####查詢db > show dbscode
local 0.078GB
####建庫及建表 > use db1排序
switched to db db1
> db.table1.insert({name:'gongjun',age:25})rem
WriteResult({ "nInserted" : 1 })
> show dbs文檔
db1 0.078GB local 0.078GB
show tablesit
system.indexes table1
####刪表 > db.table1.drop()table
true
> show tables;object
system.indexes
####刪庫 #當前的庫 > db.dropDatabase()date
{ "dropped" : "db1", "ok" : 1 }
####插入數據im
> db.table1.insert({name:'gongjun',age:25})數據
WriteResult({ "nInserted" : 1 })
> db.table1.find()
{ "_id" : ObjectId("570101edd2aad932b6e8d83f"), "name" : "gongjun", "age" : 25 }
> document={name:'world',age:55}
{ "name" : "world", "age" : 55 }
> db.table1.insert(document)
WriteResult({ "nInserted" : 1 })
> db.table1.find()
{ "_id" : ObjectId("570101edd2aad932b6e8d83f"), "name" : "gongjun", "age" : 25 } { "_id" : ObjectId("570102b5d2aad932b6e8d842"), "name" : "world", "age" : 55 }
####更新文檔 #####update > db.table1.update({name:'gongjun',age:25},{$set:{'name':'gongjun1',age:26}},{upsert:true,multi:true}) #upsert:不存在則爲insert #multi:默認false,只更新第一個文檔
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.table1.find()
{ "_id" : ObjectId("57010327d2aad932b6e8d843"), "name" : "gongjun1", "age" : 26 } { "_id" : ObjectId("5701039cd2aad932b6e8d844"), "name" : "world", "age" : 55 }
#####save 替換objectid:5701039cd2aad932b6e8d844的文檔 > db.table1.save({ "_id" : ObjectId("5701039cd2aad932b6e8d844"), "name" : "hello,world", "age" : 56 })
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.table1.find()
{ "_id" : ObjectId("57010327d2aad932b6e8d843"), "name" : "gongjun1", "age" : 26 } { "_id" : ObjectId("5701039cd2aad932b6e8d844"), "name" : "hello,world", "age" : 56 }
####刪除數據 > db.table1.remove({name:'hello,world'},{justone:true}) #justone:最多刪除一條,默認false
WriteResult({ "nRemoved" : 1 })
> db.table1.find()
{ "_id" : ObjectId("57010327d2aad932b6e8d843"), "name" : "gongjun1", "age" : 26 }
####查詢 > db.table1.find()
{ "_id" : ObjectId("57010327d2aad932b6e8d843"), "name" : "gongjun1", "age" : 26 } { "_id" : ObjectId("57010785d2aad932b6e8d845"), "name" : "hello", "age" : 123 } { "_id" : ObjectId("5701078cd2aad932b6e8d846"), "name" : "world", "age" : 12 } { "_id" : ObjectId("57010796d2aad932b6e8d847"), "name" : "jia", "age" : 45 } { "_id" : ObjectId("5701079fd2aad932b6e8d848"), "name" : "yi", "age" : 32 } { "_id" : ObjectId("570107d4d2aad932b6e8d849"), "name" : "jia", "age" : 45 }
> db.table1.find({age:45})
{ "_id" : ObjectId("57010796d2aad932b6e8d847"), "name" : "jia", "age" : 45 } { "_id" : ObjectId("570107d4d2aad932b6e8d849"), "name" : "jia", "age" : 45 }
> db.table1.find({age:45}).limit(1) #查詢一條
{ "_id" : ObjectId("57010796d2aad932b6e8d847"), "name" : "jia", "age" : 45 }
> db.table1.find({$or:[{age:45},{name:'gongjun1'}]}) #or條件查詢age爲45或name爲gongjun1
{ "_id" : ObjectId("57010327d2aad932b6e8d843"), "name" : "gongjun1", "age" : 26 } { "_id" : ObjectId("57010796d2aad932b6e8d847"), "name" : "jia", "age" : 45 } { "_id" : ObjectId("570107d4d2aad932b6e8d849"), "name" : "jia", "age" : 45 }
> db.table1.find({$or:[{name:'jia'},{name:'bin'},{name:'gongjun1'}],age:{$gt:10}}) #查詢name在jia,bin,gongjun1而且age>10的文檔
{ "_id" : ObjectId("57010327d2aad932b6e8d843"), "name" : "gongjun1", "age" : 26 } { "_id" : ObjectId("57010796d2aad932b6e8d847"), "name" : "jia", "age" : 45 } { "_id" : ObjectId("570107d4d2aad932b6e8d849"), "name" : "jia", "age" : 45 }
####排序 > db.table1.find().sort({name:1,age:-1}) #1:生序排列,-1:降序排列
{ "_id" : ObjectId("57010327d2aad932b6e8d843"), "name" : "gongjun1", "age" : 26 } { "_id" : ObjectId("57010785d2aad932b6e8d845"), "name" : "hello", "age" : 123 } { "_id" : ObjectId("57010b3ed2aad932b6e8d84a"), "name" : "jia", "age" : "65" } { "_id" : ObjectId("57010796d2aad932b6e8d847"), "name" : "jia", "age" : 45 } { "_id" : ObjectId("570107d4d2aad932b6e8d849"), "name" : "jia", "age" : 45 } { "_id" : ObjectId("5701078cd2aad932b6e8d846"), "name" : "world", "age" : 12 } { "_id" : ObjectId("5701079fd2aad932b6e8d848"), "name" : "yi", "age" : 32 }