初识MongoDB

 
 

MongoDB

Win安装

安装路径的根路径下,新建data/db文件夹

运行bin/mongod.exe启动MongoDB Server

运行bin/mongo.exe启动MongoDB命令行

常用命令

1.mongo

2.show dbs

3.use XXX

4.db.dropDatabase()

5.db.getName()

6.db.help()

7.db.copyDatabase(‘name’,’rename’)

8.show collections

9.db.createCollection(‘CollectionName’,{

capped:true//固定集合的大小,超出固定大小,先入先删

,autoIndexId:true//默认false ,自动设置id

,sizi:1000 //配合capped使用,指定集合大小

,max:1000//限定集合上限

})//第二个对象参数可缺省

10.db.xxxxxxCollection.renameCollection(‘renameName’)

11.db.xxxxxxCollection.drop()

12.db.xxxxxxCollection.insert({‘xxxxx’:’xxxxx’});

                                          Insert([{‘xxxxx’:’xxxxx’},{‘xxxxx’:’xxxxx’}])

                                          save([{‘xxxxx’:’xxxxx’},{‘xxxxx’:’xxxxx’}])

                                          insertMany([{‘xxxxx’:’xxxxx’},{‘xxxxx’:’xxxxx’}])

                                          insertOne({‘xxxxx’:’xxxxxxx’})

13.db.xxxxxxCollection.find()

                                          findOne()//查询第一条

                                          find({‘xxxxx’:’xxxxxx’})//条件查询

                                          find({},{‘age’:1})

                                          Find({‘age’:{‘$gt’:20,’$lte’:40}})//大于$gt小于$lt等于$eq大于等于$gte

                                          Find({‘age’:{‘$in’:[28,29,10]}})//$nin取反

                                          Find({‘$or’:[{‘age’:28},{‘user’:’liuxaing’}]})//$and 且

                                          Find({‘age’:{‘$not’:{‘$in’:[29,80,11]}}})

                                          Find({‘comments’:{‘$all’:[{‘xxxx’:’xxxx’}]}})

                                          Find({‘comments’:{‘$size’:3}})

                                          Find({‘user’:’liuxiang’},{‘comments’:{‘$slice’:2}})//正数从前,负数从后

                                          Find({‘vals’:{‘$elemMatch’:{‘&gt’:1,’&lt’:3}}})//elemMatch只匹配数组

                                          Find({‘name.f’:’王’,’name.e’:’五’})//文档对象总的对象的查询

                                          Find().limit(3)

                                          Find().skip(2)//忽略

                                          Find().sort({‘age’:1})//排序-1倒叙

                                         

14.db.xxxxxxCollection.update({‘xxxxx’:’xxxxxx’},{‘$set’:{‘yyyyy’:’yyyyyy’}},

true//可缺省,检查是否存在,若不存在则新增,默认false

,true//可缺省,开启多数据(批量)模式,默认false

)//更新

15.db.xxxxxxCollection.update({‘xxxxx’:’xxxxxx’},{‘$inc’:{‘yyyyy’:3}})//自增,针对数字

16.var common = {}

 db.xxxxxxCollection.update({‘xxxxx’:’xxxxxx’},{‘$push’:{‘yyyyy’:common }})//push对象入数组

17.var common = []

db.xxxxxxCollection.update({‘xxxx’:’xxxx’},{‘$push’:{‘yyyy’:{‘$each’,common}}})//批量push

18.var common = []

db.xxxxxxCollection.update({‘xxxx’:’xxxx’},{‘$push’:{‘yyyy’:{‘$each’,common,’$slice’:-3}}})

19.var comment = []

db.xxxxxxCollection.update({‘xxxxx’:’xxxx’},(‘$addToSet’:{‘yyyy’:{‘$each’:common}}))//批量 去重

20.db.xxxxxxCollection.update({‘xxxx’:’xxxx’},{‘$pop’:{‘yyyy’:-1}})//删除yyyy数组中值,负数从前删除

21.db.xxxxxxCollection.update({‘xxxx’:’xxxx’},{‘$pull’:{‘yyyy’:{‘zzzz’:’zzzz’}}})//条件删除

22.db.xxxxxxCollection.update({‘comments.yyyy’:’xxxx’},{‘$set’:{‘comments.$.yyyy’:’zzzz’}})//下表操作符comments.2.yyyy    $适配器

23.db.xxxxxxCollection.ensureIndex({‘age’:1,’name’:1})//添加索引

24.db.xxxxxxCollection.getIndexes()//获取所有索引值

25.db.xxxxxxCollection.find().explain(‘executionStats’)//性能检测

Node连接mongoDb实例

项目依赖建议使用MongoDB   2.2.33

 
var mongo = require(‘mongodb‘)
var Server = mongo.Server
var Db = mongo.Db
 
var conf ={
    adr:‘127.0.0.1‘,
    port:‘27017‘,
    auto_:{auto_reconnect:true},
    db:‘liuxiang‘
}
 
var server = new Server(conf.adr,conf.port,conf.auto_)
var db = new Db(conf.db,server)
/* function openC(colName,fun){
    db.open((err) => {
        if(!err) {
            db.collection(colName,{safe:true},function(err,col){
                    if(!err){
                        fun(col)
                        console.log(‘ok‘)
                    }else{
                        console.log(err)
                    }
            })
            console.log(‘success‘)
        }
        else {
            console.log(err)
        }
        db.close()
    })
} */
/* openC(‘liuxCo‘,function(col){
    col.find().toArray(function(err,data){
        if(!err){
            console.log(data+‘--------‘)
        }
        else{
            console.log(err)
        }
    })
}) */
 
/* openC(‘liuxCo‘,function(col){
    col.findOne(function(err,data){
        if(!err){
            console.log(data+‘===========‘)
        }
        else{
            console.log(err)
        }
    })
}) */
//公共方法
function sqlFun(colName,funName,fun_,order,order2){
    function fun(col_){
        switch(funName){
            case ‘findOne‘:
                col_.findOne((err,col) => {
                    if(!err){
                        fun_(col)
                    }else{
                        fun_(err)
                    }
                })
                break
            case ‘find‘:
                col_.find().toArray(function(err,data){
                    if(!err){
                        fun_(data)
                    }
                    else{
                        fun_(err)
                    }
                })
                break
            case ‘insert‘:
                col_.insert(order,(err,data) => {
                    if(!err) {
                        fun_(data)
                    }else {
                        fun(err)
                    }
                })
                break
            case ‘remove‘:
                col_.remove(order,(err,data) => {
                    if(!err) {
                        fun_(data)
                    }else {
                        fun_(err)
                    }
                })
                break
            case ‘update‘:
                col_.update(order,order2,(err,data) => {
                    if(!err) {
                        fun_(data)
                    }else {
                        fun_(err)
                    }
                })
                break
        }
    }
    db.open((err) => {
        if(!err) {
            db.collection(colName,{safe:true},function(err,col){
                    if(!err){
                        console.log(‘ok‘)
                        fun(col)
                    }else{
                        console.log(err)
                    }
            })
            console.log(‘success‘)
        }
        else {
            console.log(err)
        }
        db.close()
    })
}
 
/* sqlFun(‘liuxCo‘,‘insert‘,(data)=>{
    console.log(data)
},{"user":"llllll"}) */
/* sqlFun(‘liuxCo‘,‘remove‘,(data)=>{
    console.log(data)
}),{"user":"llllll"}) */
/* sqlFun(‘liuxCo‘,‘find‘,(data)=>{
    console.log(data)
}) */
//sqlFun(‘liuxCo‘,‘update‘,(data)=>{console.log(data)},{‘user‘:‘jiangge‘},{‘age‘:35})
 
/* exports.sqlFun = sqlFun */

初识MongoDB

上一篇:36 DBA命令


下一篇:MySQL DATEDIFF函数:获取两个日期之间的时间间隔