我爱编程

0812_作业Mongodb学习使用手册

2016-08-13  本文已影响64人  天才在战斗

认识mongodb

mongodb简介

MongoDB 是一个基于分布式文件存储的数据库。由C++语言编写。旨在为WEB应用提供可扩展的高性能数据存储解决方案。

MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。他支持的数据结构非常松散,是类似json的bson格式,因此可以存储比较复杂的数据类型。Mongo最大的特点是他支持的查询语言非常强大,其语法有点类似于面向对象的查询语言,几乎可以实现类似关系数据库单表查询的绝大部分功能,而且还支持对数据建立索引。

mongodb特点

它的特点是高性能、易部署、易使用,存储数据非常方便。主要功能特性有:

mongodb安装(Ubuntu32位机)

mongodb安装总结

mongodb操作

启动mongodb

run 直接启动:
例如:mongod run

--dbpath 指定存储目录启动:
例如:./mongod --dbpath=../lilei_db

--port 指定端口启动:(默认端口是:27017)
例如:mongod --port 12345。

关闭mongodb

在窗口模式中,可以直接使用Ctrl+C停止服务。

mongodb基本操作

语句 含义
show dbs 查询所有数据库列表
db 查看当前连接在哪个数据库下面
use test 切换到test数据库下面
show collections 查看test下有哪些表或者叫collection
help 查看mongodb支持哪些命令
db.help(); 查看当前数据库支持哪些方法
db.user.help();(user为表名) 查看当前数据库下的表或者表collection支持哪些方法
db.addUser(username,password) 添加用户
db.auth(usrename,password) 设置数据库连接验证
db.cloneDataBase(fromhost) 从目标服务器克隆一个数据库
db.commandHelp(name) returns the help for the command
db.copyDatabase(fromdb,todb,fromhost) 复制数据库fromdb---源数据库名称,todb---目标数据库名称,fromhost---源数据库服务器地址
db.createCollection(name,{size:3333,capped:333,max:88888}) 创建一个数据集,相当于一个表
db.currentOp() 取消当前库的当前操作
db.dropDataBase() 删除当前数据
db.eval(func,args) run code server-side
db.getCollection(cname) 取得一个数据集合,同用法:db['cname'] or
db.getCollenctionNames() 取得所有数据集合的名称列表
db.getLastErrorObj() 返回最后一个错误的对象
db.getMongo() 取得当前服务器的连接对象get the server
db.getMondo().setSlaveOk() allow this connection to read from then nonmaster membr of a replica pair
db.getName() 返回当前操作数据库的名称
db.getPrevError() 返回上一个错误对象
db.getReplicationInfo() 获得重复的数据
db.getSisterDB(name) get the db at the same server as this onew
db.killOp() 停止(杀死)在当前库的当前操作
db.printCollectionStats() 返回当前库的数据集状态
db.printReplicationInfo()
db.printSlaveReplicationInfo()
db.printShardingStatus() 返回当前数据库是否为共享数据库
db.removeUser(username) 删除用户
db.repairDatabase() 修复当前数据库
db.resetError()
db.runCommand(cmdObj) run a database command. if cmdObj is a string, turns it into {cmdObj:1}
db.setProfilingLevel(level) 0=off,1=slow,2=all
db.shutdownServer() 关闭当前服务程序
db.version() 返回当前程序的版本信息

mongodb增删改查语句

db.user.innert(
    {
      "name":"liwei",
      "tel":18843436650
    }
  );
db.user.innert(
  {
    "name":"liwei",
    "tel":18843436650,
    "yz":"100"
  }
);

db.user.innertMany(
  [
    {
      "name":"longdage",
      "sex":1,
      "jushu":"good"
    },
    {
      "name":"gaoluofeng",
      "zhiwu":"1ge",
      "jushu":"yiliu"
    }
  ]
);

db.test.remove({})                 <==> delete * from test

db.test.remove({'age':20})         <==> delete test where age=20

db.test.remove({'age':{$lt:20}})   <==> elete test where age<20

db.test.remove({'age':{$lte:20}})  <==> delete test where age<=20

db.test.remove({'age':{$gt:20}})  <==> delete test where age>20

db.test.remove({'age':{$gte:20}}) <==> delete test where age>=20

db.test.remove({'age':{$ne:20}})  <==> delete test where age!=20

test.update({'name':'foobar'},{$set:{'age':36}}) <==> update test set age=36 where name='foobar'

db.test.update({'name':'foobar'},{$inc:{'age':3}}) <==> update test set age=age+3 where name='foobar'

语句 含义
db.user.find()[.toArray()或.prety()];
db.foo.find(...).count()
db.foo.find(...).limit(n) 根据条件查找数据并返回指定记录数
db.foo.find(...).skip(n)
db.foo.find(...).sort(...) 查找排序
db.foo.findOne([query]) 根据条件查询只查询一条数据

高级查询

1.这六个就不用解释了,最常用的,也是最简单的。
// 大于  : field > value
db.collection.find({ "field" : { $gt: value } } )   

// 小于  :  field < value
db.collection.find({ "field" : { $lt: value } } )   

// 大于等于 : field >= value
db.collection.find({ "field" : { $gte: value } } )  

// 小于等于 : field <= value
db.collection.find({ "field" : { $lte: value } } )  

// 不等于 : field != value
db.things.find( { "field" : { $ne : value } } )

// 条件相当于a % 10 == 1 即a除以10余数为1的。
db.things.find( { a : { $mod : [ 10 , 1 ] } } )

2.如果要同时满足多个条件
// value1 < field < value
db.collection.find({ "field" : { $gt: value1, $lt: value2 } } )

3.不属于,属于,全部属于
// 条件相当于 j 不等于 [2,4,6] 中的任何一个。
db.things.find({j:{$nin: [2,4,6]}})

// 条件相当于j等于[2,4,6]中的任何一个。
db.things.find({j:{$in: [2,4,6]}})

// 与$in类似,但必须是[]的值全部都存在。
db.things.find( { a: { $all: [ 2, 3 ] } } )
上一篇 下一篇

猜你喜欢

热点阅读