MongoDB
MongoDB是一个基于分布式文件存储的数据库。由C++语言编写。旨在为WEB应用提供可扩展的高性能数据存储解决方案。也是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。它支持的数据结构非常松散,是类似json的bson格式,因此可以存储比较复杂的数据类型。Mongo最大的特点是它支持的查询语言非常强大,其语法有点类似于面向对象的查询语言,几乎可以实现类似关系数据库单表查询的绝大部分功能,而且还支持对数据建立索引。
帮助文档
简单实例
> show dbs //图1.1
admin 0.000GB
config 0.000GB
local 0.000GB
test 0.000GB
> use person
switched to db person
> db.person.insert({"name":"lilong","age":30})
WriteResult({ "nInserted" : 1 })
> db.person.insert({"name":"bill","age":25})
WriteResult({ "nInserted" : 1 })
> show dbs //图1.2
admin 0.000GB
config 0.000GB
local 0.000GB
person 0.000GB
test 0.000GB
> db.person.update({"name":"lilong"},{"$set":{"email":"123@qq.com"}})//图1.3
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.person.find() //图1.4
{ "_id" : ObjectId("5cd965e0f1cfbae0356d8804"), "name" : "lilong", "age" : 30, "email" : "123@qq.com" }
{ "_id" : ObjectId("5cd965e0f1cfbae0356d8805"), "name" : "bill", "age" : 25 }
> db.person.update({"name":"lilong" },{"$set":{"name":"lee"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.person.findOne() //图1.5
{
"_id" : ObjectId("5cd965e0f1cfbae0356d8804"),
"name" : "lee",
"age" : 30,
"email" : "123@qq.com"
}
> db.person.update({},{$unset:{email:""}},false,true)
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 1 })
> db.person.find() //图1.6
{ "_id" : ObjectId("5cd965e0f1cfbae0356d8804"), "name" : "lee", "age" : 30 }
{ "_id" : ObjectId("5cd965e0f1cfbae0356d8805"), "name" : "bill", "age" : 25 }
> db.person.remove({age:25})
WriteResult({ "nRemoved" : 1 })
> db.person.find() //图1.7
{ "_id" : ObjectId("5cd965e0f1cfbae0356d8804"), "name" : "lee", "age" : 30 }
> use person
switched to db person
> db.dropDatabase()//图1.8
{ "dropped" : "person", "ok" : 1 }
> db.help() //图1.9
DB methods:
db.adminCommand(nameOrDocument) - switches to 'admin' db, and runs command [just calls db.runCommand(...)]
db.aggregate([pipeline], {options}) - performs a collectionless aggregation on this database; returns a cursor
db.auth(username, password)
db.cloneDatabase(fromhost)
db.commandHelp(name) returns the help for the command
db.copyDatabase(fromdb, todb, fromhost)
db.createCollection(name, {size: ..., capped: ..., max: ...})
db.createView(name, viewOn, [{$operator: {...}}, ...], {viewOptions})
db.createUser(userDocument)
db.currentOp() displays currently executing operations in the db
db.dropDatabase()
db.eval() - deprecated
db.fsyncLock() flush data to disk and lock server for backups
db.fsyncUnlock() unlocks server following a db.fsyncLock()
db.getCollection(cname) same as db['cname'] or db.cname
db.getCollectionInfos([filter]) - returns a list that contains the names and options of the db's collections
db.getCollectionNames()
db.getLastError() - just returns the err msg string
db.getLastErrorObj() - return full status object
db.getLogComponents()
db.getMongo() get the server connection object
db.getMongo().setSlaveOk() allow queries on a replication slave server
db.getName()
db.getPrevError()
db.getProfilingLevel() - deprecated
db.getProfilingStatus() - returns if profiling is on and slow threshold
db.getReplicationInfo()
db.getSiblingDB(name) get the db at the same server as this one
db.getWriteConcern() - returns the write concern used for any operations on this db, inherited from server object if set
db.hostInfo() get details about the server's host
db.isMaster() check replica primary status
db.killOp(opid) kills the current operation in the db
db.listCommands() lists all the db commands
db.loadServerScripts() loads all the scripts in db.system.js
db.logout()
db.printCollectionStats()
db.printReplicationInfo()
db.printShardingStatus()
db.printSlaveReplicationInfo()
db.dropUser(username)
db.repairDatabase()
db.resetError()
db.runCommand(cmdObj) run a database command. if cmdObj is a string, turns it into {cmdObj: 1}
db.serverStatus()
db.setLogLevel(level,<component>)
db.setProfilingLevel(level,slowms) 0=off 1=slow 2=all
db.setWriteConcern(<write concern doc>) - sets the write concern for writes to the db
db.unsetWriteConcern(<write concern doc>) - unsets the write concern for writes to the db
db.setVerboseShell(flag) display extra information in shell output
db.shutdownServer()
db.stats()
db.version() current version of the server
> use mydb
switched to db mydb
> db.person.insertMany([{"name":"ann","age":35},{"name":"li","age":26},{"name":"chen","age":24}])
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("5cd965e0f1cfbae0356d8806"),
ObjectId("5cd965e0f1cfbae0356d8807"),
ObjectId("5cd965e0f1cfbae0356d8808")
]
}
> db.person.find() //图2.1
{ "_id" : ObjectId("5cd965e0f1cfbae0356d8806"), "name" : "ann", "age" : 35 }
{ "_id" : ObjectId("5cd965e0f1cfbae0356d8807"), "name" : "li", "age" : 26 }
{ "_id" : ObjectId("5cd965e0f1cfbae0356d8808"), "name" : "chen", "age" : 24 }
> db.person.find({"age":{"$lt":25,"$gt":0}}) //图2.2
{ "_id" : ObjectId("5cd965e0f1cfbae0356d8808"), "name" : "chen", "age" : 24 }
> db.person.find({"name":{"$ne": "li"} }) //图2.3
{ "_id" : ObjectId("5cd965e0f1cfbae0356d8806"), "name" : "ann", "age" : 35 }
{ "_id" : ObjectId("5cd965e0f1cfbae0356d8808"), "name" : "chen", "age" : 24 }
> db.person.find({"age":{ $in:[ 26, 35] }}) //图2.4
{ "_id" : ObjectId("5cd965e0f1cfbae0356d8806"), "name" : "ann", "age" : 35 }
{ "_id" : ObjectId("5cd965e0f1cfbae0356d8807"), "name" : "li", "age" : 26 }
> db.person.find({"$or":[{"age":30},{"name":"ann"}]}) //图2.5
{ "_id" : ObjectId("5cd965e0f1cfbae0356d8806"), "name" : "ann", "age" : 35 }
> db.person.find({"age":{$not:/30/},"name":{$not:/ann/}}) //图2.6
{ "_id" : ObjectId("5cd965e0f1cfbae0356d8807"), "name" : "li", "age" : 26 }
{ "_id" : ObjectId("5cd965e0f1cfbae0356d8808"), "name" : "chen", "age" : 24 }
> db.person.insert({"name":"","age":15})
WriteResult({ "nInserted" : 1 })
> db.person.insert({"":"cherry","age":15})
WriteResult({ "nInserted" : 1 })
> db.person.find({"name":null}) //图2.7
{ "_id" : ObjectId("5cd965e0f1cfbae0356d880a"), "" : "cherry", "age" : 15 }
> db.person.insert({"name":"Chen","age":27})
WriteResult({ "nInserted" : 1 })
> db.person.find({"name":/chen/i }) //图2.8
{ "_id" : ObjectId("5cd965e0f1cfbae0356d8808"), "name" : "chen", "age" : 24 }
{ "_id" : ObjectId("5cd965e0f1cfbae0356d880b"), "name" : "Chen", "age" : 27 }
> db.food.insert({"fruit":["apple","banana","peach"]})
WriteResult({ "nInserted" : 1 })
> db.food.find({"fruit":"banana"}) //图2.9
{ "_id" : ObjectId("5cd965e0f1cfbae0356d880c"), "fruit" : [ "apple", "banana", "peach" ] }
> db.food.insert({"_id":1,"fruit":["apple","banana","peach"]})
WriteResult({ "nInserted" : 1 })
> db.food.insert({"_id":2,"fruit":["apple","kumquat","orange"]})
WriteResult({ "nInserted" : 1 })
> db.food.insert({"_id":3,"fruit":["cherry","banana","apple"]})
WriteResult({ "nInserted" : 1 })
> db.food.find({fruit:{"$all":["banana","apple"]}}) //图2.10
{ "_id" : ObjectId("5cd965e0f1cfbae0356d880c"), "fruit" : [ "apple", "banana", "peach" ] }
{ "_id" : 1, "fruit" : [ "apple", "banana", "peach" ] }
{ "_id" : 3, "fruit" : [ "cherry", "banana", "apple" ] }
> db.food.find({fruit:{$size:3}}) //图2.11
{ "_id" : ObjectId("5cd965e0f1cfbae0356d880c"), "fruit" : [ "apple", "banana", "peach" ] }
{ "_id" : 1, "fruit" : [ "apple", "banana", "peach" ] }
{ "_id" : 2, "fruit" : [ "apple", "kumquat", "orange" ] }
{ "_id" : 3, "fruit" : [ "cherry", "banana", "apple" ] }
> db.person.update({"name":"ann"},{$set:{"name":{"firstname":"anjel","lastname":"ana"}}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.person.find({"name":{firstname:"anjel",lastname:"ana"}}) //图2.12
{ "_id" : ObjectId("5cd965e0f1cfbae0356d8806"), "name" : { "firstname" : "anjel", "lastname" : "ana" }, "age" : 35 }