mongodb数据库操作
查看有多少数据库
> show dbs
admin0.000GB
local0.000GB
切换数据库
>use admin
switchedtodb admin
在mongodb数据库中不需要对数据库进行创建,在需要创建数据库时mongodb会自动创建
插入数据,创建数据库插入数据
db.imooc_collection.insert({x:1})
插入数据格式为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
查看创建的表
> show collectionsimooc_collection
查看插入的数据
_id : mongodb自动生成的ID全局范围内不能重复
> 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}
插入多条数据
>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}
数据的更新
> 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}
更新多条数据
> 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}
简书怎么使用markdown语法?