Mongodb 安装和启动

一.首先去官网下载对应的的mongodb ,本人的操作系统是win7 64位

  mongodb-win32-x86_64-2.0.6.rar   

解压安装:进入到bin目录下,会看到N多的.exe文件

  

二.启动mongodb mongod.exe --dbpath=d:mongodatadb --logpath=d:mongologlog.txt,

  通过浏览器访问localhost:27017;如果显示"

You are trying to access MongoDB on the native driver port. For http diagnostic access, add 1000 to the port number

"就将端口改成28017,表示mongodb服务端已经成功开启

三.重新打开cmd,进入到bin目录下,点击mongo 即进入了客户端,开始你的命令吧!

四.mongo命令操作

  1. help 和 db.help(),通过help命令和db.help()来查看mongo的各种命令。

  2.库的操作   

    show dbs   //显示所有的数据库

    use database_name  //切换数据库和使用数据库

    db.dropDatabase()  //删除数据库

    db.repaireDatabase() //修复当前数据库
    
    db.getName() //获取数据库名称

    db.stats()     //获取数据库信息

    db.version() //数据库版本

    db.getMongo() //当前db链接服务器地址

   3.数据表操作 

   

        show collections //查看当前库的所有集合
        
        db.createCollection('tableName') //创建一个集合

        db.getCollection('tableName') //获取集合的信息

     db.collectionName.drop() //删除集合
     
     db.collectionName.renameCollection('newName'); //重命名集合

 

  4.用户操作

db.addUser('name','password',true) //添加用户
show users  //查看用户

 

  5.集合数据操作

    

db.collection.insert('name':'ikasa',age:20) //插入一条数据

db.collection.find() //查询说有记录 

db.collection.find({age:20}) //age= 20

db.collection.find({age:{$gt:20}}) //age >20   [$lt=> '<' ,$gte=>'>=']

db.collection.find({age:{$gt:20,$lte:30}}) //>20 and <=30

db.collection.find({name:'name',age:{$gt:20}}) //name='name' and age>20

db.collection.find({$or:[{age:20},{name:ikasa}]})  // age= 20 or name =ikasa

db.collection.find({name:/name/})  // like %name%

db.collection.find({name:/^name/})  //like name%

db.collection.find({name:/name$/})  // like %name

db.collection.find().count();

db.collection.find().sort({age:1})  // order by age desc

db.collection.find().limit(5)
db.collection.find().limit(5).skip(5)  //limit(5,5)



db.collection.distinct() // 去除重复的数据列

db.collection.find({},{name:1})   //查询字段name

//删除

db.user.remove({age:20})

//更新
db.user.update({name:name},{$set:{age:30}},false,true) //update set age=30 where name = name

//添加
db.user.insert({name:name})

  

原文地址:https://www.cnblogs.com/ikasa007/p/4435237.html