`
zhaohaolin
  • 浏览: 985980 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

MongoDB基本介绍及一些用法

 
阅读更多

面向文档的非关系数据库主要解决的问题不是高性能的并发读写,而是保证海量数据存储的同时,具有良好的查询性能。MongoDB是用C++开发的,而CouchDB则是Erlang开发的:

  MongoDB

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

  Mongo主要解决的是海量数据的访问效率问题,根据官方的文档,当数据量达到50GB以上的时候,Mongo的数据库访问速度是MySQL的 10倍以上。Mongo的并发读写效率不是特别出色,根据官方提供的性能测试表明,大约每秒可以处理0.5万-1.5次读写请求。对于Mongo的并发读 写性能,我(robbin)也打算有空的时候好好测试一下。

  因为Mongo主要是支持海量数据存储的,所以Mongo还自带了一个出色的分布式文件系统GridFS,可以支持海量的数据存储,但我也看到有些评论认为GridFS性能不佳,这一点还是有待亲自做点测试来验证了。

  最后由于Mongo可以支持复杂的数据结构,而且带有强大的数据查询功能,因此非常受到欢迎,很多项目都考虑用MongoDB来替代MySQL来实现不是 特别复杂的Web应用,比方说why we migrated from MySQL to MongoDB就是一个真实的从MySQL迁移到MongoDB的案例,由于数据量实在太大,所以迁移到了Mongo上面,数据查询的速度得到了非常显著 的提升。

  MongoDB也有一个ruby的项目MongoMapper,是模仿Merb的DataMapper编写的MongoDB的接口,使用起来非常简单,几乎和DataMapper一模一样,功能非常强大易用。

  MongoDB语法:

  启动服务

  mongod.exe –dbpath F:\DataBase\MongoDB\db\

  –dbpath 数据文件存放路径

  –port 数据服务端口

  启动客户端

  mongo.exe cclove

  cclove 所连接的数据库名称

  数据库操作语法

  mongo –path

  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.cname

  db.getCollenctionNames() 取得所有数据集合的名称列表

  db.getLastError() 返回最后一个错误的提示消息

  db.getLastErrorObj() 返回最后一个错误的对象

  db.getMongo() 取得当前服务器的连接对象get the server connection object

  db.getMondo().setSlaveOk() allow this connection to read from then nonmaster membr of a replica pair

  db.getName() 返回当操作数据库的名称

  db.getPrevError() 返回上一个错误对象

  db.getProfilingLevel() ?什么等级

  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() 返回当前程序的版本信息

  数据集(表)操作语法

  db.linlin.find({id:10}) 返回linlin数据集ID=10的数据集

  db.linlin.find({id:10}).count() 返回linlin数据集ID=10的数据总数

  db.linlin.find({id:10}).limit(2) 返回linlin数据集ID=10的数据集从第二条开始的数据集

  db.linlin.find({id:10}).skip(8) 返回linlin数据集ID=10的数据集从0到第八条的数据集

  db.linlin.find({id:10}).limit(2).skip(8) 返回linlin数据集ID=1=的数据集从第二条到第八条的数据

  db.linlin.find({id:10}).sort() 返回linlin数据集ID=10的排序数据集

  db.linlin.findOne([query]) 返回符合条件的一条数据

  db.linlin.getDB() 返回此数据集所属的数据库名称

  db.linlin.getIndexes() 返回些数据集的索引信息

  db.linlin.group({key:…,initial:…,reduce:…[,cond:...]})

  db.linlin.mapReduce(mayFunction,reduceFunction,)

  db.linlin.remove(query) 在数据集中删除一条数据

  db.linlin.renameCollection(newName) 重命名些数据集名称

  db.linlin.save(obj) 往数据集中插入一条数据

  db.linlin.stats() 返回此数据集的状态

  db.linlin.storageSize() 返回此数据集的存储大小

  db.linlin.totalIndexSize() 返回此数据集的索引文件大小

  db.linlin.totalSize() 返回些数据集的总大小

  db.linlin.update(query,object[,upsert_bool]) 在此数据集中更新一条数据

  db.linlin.validate() 验证此数据集

  db.linlin.getShardVersion() 返回数据集共享版本号

  db.linlin.find({‘name’:'foobar’}) select * from linlin where name=’foobar’

  db.linlin.find() select * from linlin

  db.linlin.find({‘ID’:10}).count() select count(*) from linlin where ID=10

  db.linlin.find().skip(10).limit(20) 从查询结果的第十条开始读20条数据 select * from linlin limit 10,20 ———-mysql

  db.linlin.find({‘ID’:{$in:[25,35,45]}}) select * from linlin where ID in (25,35,45)

  db.linlin.find().sort({‘ID’:-1}) select * from linlin order by ID desc

  db.linlin.distinct(‘name’,{‘ID’:{$lt:20}}) select distinct(name) from linlin where ID<20

  db.linlin.group({key:{‘name’:true},cond:{‘name’:'foo’},reduce:function(obj,prev){prev.msum+=obj.marks;},initial:{msum:0}})

  select name,sum(marks) from linlin group by name

  db.linlin.find(‘this.ID<20′,{name:1}) select name from linlin where ID<20

  db.linlin.insert({‘name’:'foobar’,'age’:25}) insert into linlin (‘name’,'age’) values(‘foobar’,25)

  db.linlin.insert({‘name’:'foobar’,'age’:25,’email’:'cclove2@163.com’})

  db.linlin.remove({}) delete * from linlin

  db.linlin.remove({‘age’:20}) delete linlin where age=20

  db.linlin.remove({‘age’:{$lt:20}}) delete linlin where age<20

  db.linlin.remove({‘age’:{$lte:20}}) delete linlin where age<=20

  db.linlin.remove({‘age’:{$gt:20}}) delete linlin where age>20

  db.linlin.remove({‘age’:{$gte:20}}) delete linlin where age>=20

  db.linlin.remove({‘age’:{$ne:20}}) delete linlin where age!=20

  db.linlin.update({‘name’:'foobar’},{$set:{‘age’:36}}) update linlin set age=36 where name=’foobar’

  db.linlin.update({‘name’:'foobar’},{$inc:{‘age’:3}}) update linlin set age=age+3 where name=’foobar’

分享到:
评论

相关推荐

    MongoDB基本操作指南

    在使用MongoDB做产品原型的过程中,我们总结了MonogDB的一些亮点:使用JSON风格语法,易于掌握和理解:MongoDB使用JSON的变种BSON作为内部存储的格式和语法。针对MongoDB的操作都使用JSON风格语法,客户端提交或接收...

    mongodb入门

    mongodb入门小书,简单快速高效!可以看看,快速了解mongodb的基本用法!

    mysql, mongodb, redis基本使用方法

    mysql, mongodb, redis三大数据库的基本是用命令, 以及举例

    MongoDB开发使用手册.docx

    MongoDB开发使用手册.docx 介绍mongodb的基本使用方法

    mongodb 数据库基本操作.doc

    以下是一些基本操作的简要说明: 连接MongoDB数据库: 使用MongoDB的官方驱动程序或第三方库来连接MongoDB。例如,在Python中,可以使用pymongo库来连接MongoDB。 连接时,需要提供MongoDB服务器的地址和端口号...

    Mongodb.chm

    MongoDB介绍及安装 通过samus驱动实现基本数据操作 在MVC模式下通过Jqgrid表格操作MongoDB数据 用MongoDB的文档结构描述数据关系 MongoDB文件存取操作 MongoDB索引用法和效率分析

    Node.js的MongoDB驱动Mongoose基本用法教程_.docx

    Node.js的MongoDB驱动Mongoose基本用法教程_.docx

    mongodb基本命令实例小结

    主要介绍了mongodb基本命令,结合实例形式总结分析了MongoDB数据库切换、查看、删除、查询等基本命令用法与操作注意事项,需要的朋友可以参考下

    C# 安装并使用 Mongodb的笔记

    C# 访问Mongodb数据库,基本进销存的使用. Mongodb 在windows7上面的安装方法.

    springMongodb参考文档中文版

    使用Spring Data MongoDB和MongoDB 3.0 17.1.1。配置选项 17.1.2。WriteConcern和WriteConcernChecking 17.1.3。认证 17.1.4。服务器端验证 17.1.5。其他事情要注意 附录 附录A:命名空间参考 元素 附录B:Poppers...

    mongodb随机获取一条记录实例

    介绍php7.0以上使用mongdb的基本方法,并提供mongodb随机获取一条记录的测试实例。

    Spring Boot使用MongoDB详解

    本文将详细介绍如何在Spring Boot项目中集成MongoDB数据库,包括其原理、使用流程、步骤以及完整的代码示例。通过本文,读者将能够了解Spring Boot与MongoDB的集成方式,掌握在Spring Boot应用程序中操作MongoDB...

    深入云计算 MongoDB管理与开发实战详解pdf.part1

    第2章 MongoDB基本原理与安装 2.1 数据库结构 2.2 文档 2.3 集合 2.3.1 集合的无模式 2.3.2 集合的命名 2.4 MongoDB数据类型 2.4.1 基本数据类型 2.4.2 数字类型 2.4.3 日期类型 2.4.4 ...

    MongoDB权威指南(中文版)高清

    856.4.2 例2:网页分类 876.4.3 MongoDB和MapReduce 87第7章 进阶指南 917.1 数据库命令 917.1.1 命令的工作原理 927.1.2 命令参考 937.2 固定集合 957.2.1 属性及用法 967.2.2 创建固定集合 967....

    Mongodb语法使用说明(含详细示例)

    虽然MongoDB本质上是一个面向文档的NoSQL数据库,但通过使用一些扩展和工具,我们仍然可以使用类似SQL的语法来执行常见的数据库操作。 文章通过丰富的示例和说明,展示了如何在MongoDB中执行基本的增删改查操作。...

    电子书:MongoDB权威指南(中文版)

    82.4 启动MongoDB 92.5 MongoDB Shell 102.5.1 运行shell 102.5.2 MongoDB 客户端 112.5.3 shell 中的基本操作 112.5.4 使用shell 的窍门 132.6 数据类型 152.6.1 基本数据类型 152.6.2 数字 172....

    Android编程连接MongoDB及增删改查等基本操作示例

    主要介绍了Android编程连接MongoDB及增删改查等基本操作,简单介绍了MongoDB功能、概念、使用方法及Android操作MongoDB数据库的基本技巧,需要的朋友可以参考下

    MongoDB 中Limit与Skip的使用方法详解

    MongoDB 中Limit与Skip的使用方法详解 一 MongoDB Limit() 方法 如果你需要在MongoDB中读取指定数量的数据记录,可以使用MongoDB的Limit方法,limit()方法接受一个数字参数,该参数指定从MongoDB中读取的记录条数。...

    springMVC+MongoDB实现基本的登录注册以及增删改查

    springMVC+MongoDB实现基本的登录注册以及增删改查 用到idea,MongoDB4.0.5版本,java8,tomcat9,spring4.0.0jar,spring-data-mongodb1.5.5jar...其中还有一个测试类,有MongoTemplate的基本方法(增删改查等)。

Global site tag (gtag.js) - Google Analytics