查看有多少數(shù)據(jù)庫(kù)
> show dbs
admin0.000GB
local0.000GB
切換數(shù)據(jù)庫(kù)
>use admin
switchedtodb admin
在mongodb數(shù)據(jù)庫(kù)中不需要對(duì)數(shù)據(jù)庫(kù)進(jìn)行創(chuàng)建,在需要?jiǎng)?chuàng)建數(shù)據(jù)庫(kù)時(shí)mongodb會(huì)自動(dòng)創(chuàng)建
插入數(shù)據(jù),創(chuàng)建數(shù)據(jù)庫(kù)插入數(shù)據(jù)
db.imooc_collection.insert({x:1})
插入數(shù)據(jù)格式為json格式
> use imoocswitched to db imooc
> db.imooc_collection.insert({x:1})WriteResult({"nInserted":1})
> show dbsadmin0.000GBimooc0.000GBlocal0.000GB
> use imoocswitched to db imooc
> db.imooc_collection.insert({x:1})WriteResult({"nInserted":1})> show dbsadmin0.000GBimooc0.000GBlocal0.000GB
> use imoocswitched to db imooc
> db.dropDatabase(){"dropped":"imooc","ok":1}
> show dbsadmin0.000GBlocal0.000GB
查看創(chuàng)建的表
> show collectionsimooc_collection
查看插入的數(shù)據(jù)
_id : mongodb自動(dòng)生成的ID全局范圍內(nèi)不能重復(fù)
> db.imooc_collection.find()
{"_id": ObjectId("586ca9a3482469b789d37a76"),"x":1}
> db.imooc_collection.insert({x:3,_id:1})
WriteResult({"nInserted":1})
> db.imooc_collection.insert({x:2,_id:1})
WriteResult({"nInserted":0,"writeError": {"code":11000,"errmsg":"E11000 duplicate key error collection: imooc.imooc_collection index: _id_ dup key: { : 1.0 }"}})
> db.imooc_collection.find()
{"_id": ObjectId("586ca9a3482469b789d37a76"),"x":1}{"_id":1,"x":3}
> db.imooc_collection.find({x:1})
{"_id": ObjectId("586ca9a3482469b789d37a76"),"x":1}
插入多條數(shù)據(jù)
>for(i=3;i<100;i++)db.imooc_collection.insert({x:i})
WriteResult({"nInserted":1})
> db.imooc_collection.find()
{"_id": ObjectId("586ca9a3482469b789d37a76"),"x":1}{"_id":1,"x":3}{"_id": ObjectId("586cac66482469b789d37a77"),"x":3}{"_id": ObjectId("586cac66482469b789d37a78"),"x":4}{"_id": ObjectId("586cac66482469b789d37a79"),"x":5}{"_id": ObjectId("586cac66482469b789d37a7a"),"x":6}{"_id": ObjectId("586cac66482469b789d37a7b"),"x":7}{"_id":?
?> db.imooc_collection.find().count()
99
?> db.imooc_collection.find().skip(3).limit(2).sort({x:1}){"_id": ObjectId("586cac66482469b789d37a78"),"x":4}{"_id": ObjectId("586cac66482469b789d37a79"),"x":5}
數(shù)據(jù)的更新
> db.imooc_collection.find({x:1})
{"_id": ObjectId("586ca9a3482469b789d37a76"),"x":1}
> db.imooc_collection.update({x:1},{x:999})
WriteResult({"nMatched":1,"nUpserted":0,"nModified":1})
> db.imooc_collection.find({x:1})
> db.imooc_collection.find({x:999})
{"_id": ObjectId("586ca9a3482469b789d37a76"),"x":999}
更新部分字段
> db.imooc_collection.insert({x:100,y:100,z:100})
WriteResult({"nInserted":1})
> db.imooc_collection.update({z:100},{$set:{y:99}})
WriteResult({"nMatched":1,"nUpserted":0,"nModified":1})
> db.imooc_collection.find({z:100})
{"_id": ObjectId("586cae313728d003e066c223"),"x":100,"y":99,"z":100}
更新不存在的字段
> db.imooc_collection.find({y:100})
> db.imooc_collection.update({y:100},{y:999})
WriteResult({"nMatched":0,"nUpserted":0,"nModified":0})
> db.imooc_collection.find({y:999})
?> db.imooc_collection.update({y:100},{y:999},true)
WriteResult({"nMatched":0,"nUpserted":1,"nModified":0,"_id": ObjectId("586caf5f31b55e5759fd031d")})
> db.imooc_collection.find({y:999})
{"_id": ObjectId("586caf5f31b55e5759fd031d"),"y":999}
更新多條數(shù)據(jù)
> db.imooc_collection.find({c:2})
{"_id": ObjectId("586cafad3728d003e066c224"),"c":2}
> db.imooc_collection.find({c:1})
{"_id": ObjectId("586cafae3728d003e066c225"),"c":1}{"_id": ObjectId("586cafaf3728d003e066c226"),"c":1}
> db.imooc_collection.update({c:1},{$set:{c:2}},false,true)
WriteResult({"nMatched":2,"nUpserted":0,"nModified":2})
> db.imooc_collection.find({c:1})
> db.imooc_collection.find({c:2})
{"_id": ObjectId("586cafad3728d003e066c224"),"c":2}
{"_id": ObjectId("586cafae3728d003e066c225"),"c":2}
{"_id": ObjectId("586cafaf3728d003e066c226"),"c":2}
簡(jiǎn)書(shū)怎么使用markdown語(yǔ)法?