美文网首页我爱编程
数据库之二 MongoDB

数据库之二 MongoDB

作者: 旅豆 | 来源:发表于2018-05-22 21:19 被阅读0次

    mongodb:菲关系型数据库(nosql)

    安装

    首先去www.mongodb.org下载压缩包

    window:

    压缩包:
    路径英文,配置环境,创建data->log|db,管理员cmd,

    mongod --dbpath "data路径"->mogo
    

    操作:注册服务

    mongod --dbpath "D:/mongodb/data/db" --logpath "D:/mongodb/data/log/log.log" --install 
    mongod --remove 移除mongod
    
    linux:

    压缩包:解压,配置

    安装:sudo apt install mongodb 
    

    数据库操作

    show dbs   查询所有数据库
    db   显示当前所在数据库
    use xx   进入xx数据库,无论xx存在与否,但不创建   
        db.xx.insert((x:""))   创建当前数据库
        db.dropDatabase()   删除当前数据库
        db.createCollection("xcx")    创建数据库Collection集合xcx
        show Collection     查看数据库下的所有集合
    

    表操作

    db.xcx.insert({名:值,};{..};..)     插入,可以多条
    db.xcx.insertOne({ })/ insertMany([{ },{ },])
    
    db.drop.xcx         删除xcx
    db.xcx.drop()       删除集合
        
    db.xcx.save({ })     存在则更新,不存在则保存
    
    db.xcx.find/ findOne/ findMany()(null/.pretty())    查询所有数据
    db.xcx.update({名:值},{$set:{名:新值,...}} (null/{multi:true}) 可更新多条,默认为false)
    db.xcx.update.updateOne/ updateMany
    
    db.xcx.remove({名:值})   删除,重复时删除多条
    db.xcx.remove({名:值}{justOne:true/1})  只删除一条
    db.xcx.deletOne/ deletMany 
    
    db.xcx.find().limit()/ skip()/ skip().limit()
    

    示例

    首先根据命令插入多条数据
    db.xcx.insert({'name':'name1','age':'age1'..};{..};..)     
    

    接着可以进行查询、删除、修改操作

    db.hero.find()
    db.hero.find().pretty()
    db.hero.find({age: 16})
    db.hero.find({$gt: {age : 20}})
    db.hero.find({age: {$gte: 20}})
    db.hero.find({age: { $lte: 20}})
    db.hero.find({$or: [{name:'xxx1'},{age : {$gt: 20}}] })
    db.hero.find({$or: [{name:'xxx1'},{age : {$gt: 40}}],name: 'xxx' })
    db.hero.find({age: {$in:[20,40,50]}})
    db.hero.find({age: {$nin:[20,25,50]}})
    db.hero.find({name: /^gao/})
    db.hero.find({name: /^g/})
    db.hero.find({name: /ei$/})
    db.hero.find({name: { $regex : "^xx"} })
    db.hero.find({name: { $regex : "^l"} })
    
    db.hero.find({},{name:1})
    db.hero.find({},{name:1,_id: 0})
    db.hero.find().sort({age:1})
    db.hero.find().sort({age:1,name:-1})
    db.hero.find().count()
    db.hero.find({age: {$gt : 20}}).count()
    db.hero.count()
    db.hero.count({age: {$lte: 25}})
    

    相关文章

      网友评论

        本文标题:数据库之二 MongoDB

        本文链接:https://www.haomeiwen.com/subject/fvlkjftx.html