您的位置:首页 > 数据库 > Mongodb

mongodb简单介绍以及使用整理1

2013-12-23 17:10 633 查看
mongodb介绍:

1,Mongodb一个基于分布式文件存储的数据库,由c++编写,旨在为web应用提供可扩展的高性能的存储解决方案

2,一个介于关系数据库和非关系数据库之间的产品,是非关系型数据库中功能最丰富,最像关系数据库的产品

3,它支持的数据结构非常松散,是类似json的 bson格式,因此可以存储比较复杂的数据类型

4,支持的查询语言强大,语法类似面向对象的查询语言,几乎可以支持类似关系数据库的单表查询的大部分功能,而且可以支持对数据建立索引,支持mapredu的功能

特点:

1,高性能,易部署,易使用,存储数据方便

2,模式自由,支持动态查询,完全索引,文档嵌套查询

3,面向集合,以文档kv形势存储,key唯一标示,value则可以是复杂的文件类型

4,支持主从服务器间的复制和故障恢复

5,自动分片,

适用:

1,网站数据,实时插入更新和查询

2,由于性能很高,可以做持久化缓存

3,存储大尺寸,低价值的数据

4,高伸缩的集群场景

5,bson格式非常适合文档化数据的存储和查询

不适应:

1,高事务性的系统(银行或会计系统)

2,传统的商业智能应用(针对特定问题的bi数据库会对产生高度优化的查询方式)

支持的数据类型:null 布尔 整数 浮点 字符串 对象id 日期 时间戳 数组 内嵌文档  正则

 

mongodb基本操作

MongoDB读操作

1) WHERE

# select * from users where name = ‘lili' > db.users.find({name : "lili"})

    { "_id" : ObjectId("502e06947dcbce905cc49831"), "name" : "lili", "sex" : "female", "age" : 18, "married" : false }

2) FIELDS

# select name, age from users where age = 18

    > db.users.find({age : 18}, {name : 1, age : 1})

    { "_id" : ObjectId("502e06947dcbce905cc49831"), "name" : "lili", "age" : 18 }

# select name, age from users > db.users.find({}, {name : 1, age : 1})

MongoDB读操作

3) SORT # select * from users order by age > db.users.find().sort({age : 1}) # select * from users order by sex asce, age desc > db.users.find().sort({sex : 1, age : -1})

4) SLICE

    # select * from users skip 2 limit 3

    > db.users.find().skip(2).limit(3)

    Conditional Operators # select * from users where sex = ‘male’and age > 18 and age < 30

     > db.users.find({sex : "male", age : {$gt : 18, $lt : 30}}) 比较操作包括:$gt (>)、$lt (<)、$gte (>=)、$lte(<=)、$ne (!=)

MongoDB读操作

5) IN # select * from users where age in (18, 28)

     > db.users.find({age : {$in : [18, 28]}}) 对应的操作符有 $nin (not in)

6) COUNT # select count(*) from users where age > 18

     > db.users.find({age : {$gt : 18}}).count()

7)OR # select * from users where age = 18 or age = 28 > db.users.find({$or : [{age : 18}, {age : 28}]}) # select * from users where age <= 18 or age >= 28

     > db.users.find({$or : [{age : {$lte : 18}}, {age : {$gte : 28}}]})

MongoDB写操作

1) INSERT # insert into users value(…)

     > db.users.insert({name : "Jimmy", sex : "male", age : 88})

2) UPDATE

     # update users set age = 100, sex = 0 where name = ‘Jimmy‘

     > db.users.update({name : "Jimmy"}, {$set : {age : 100, sex : "female"}})

      update(criteria, objNew, upsert, mul) 有几个参数需要注意

      criteria: 需要被更新的条件表达式

      objNew: 更新表达式

      upsert: 如目标记录不存在,是否插入新文档

      multi: 是否更新多个文档

MongoDB写操作

2) UPDATE

    # update users set age = age + 10

    > db.users.update({}, {$inc:{age:10}}, false, true)

    # update users set age = age + 10, sex = ‘male’ where name = ‘Joe‘

    > db.users.update({name : "Joe"}, {$inc : {age: 10}})

3)REMOVE

    用于删除单个或全部文档,删除后的文档无法恢复

    > id = db.users.findOne({name : "lili"})._id > db.users.remove(id)

    > db.users.remove()//移除所有

MongoDB高级操作

Distinct

    > db.users.distinct("name")

    [ "Joe", "lili", "Jimmy", "Jeremy" ]

Group

    #SELECT name, sum(marks) FROM user where name='foo'  GROUP BY name >db.user.group({ key : {‘name’ : true}, cond: {‘name’ : ‘foo’}, reduce: function(obj,prev) { prev.msum += obj.marks; }, initial: {msum : 0} });

Index

     // single ascending

     >db.colors.ensureIndex({name: 1})

     // unique

     > db.colors.ensureIndex({email: 1}, {unique: true})

     // single descending

     > db.colors.ensureIndex({createdat: -1})

     // compound

    > db.colors.ensureIndex({name: 1, createdat: -1})

 

 

副本集操作:

Primary主服务器,写操作只能在其身上发生,通过保存操作日志(oplog),然后异步同步到多个 Secondary 上。

Secondary从服务器,热机备份主服务器上的数据,分担主机读压力,当主机故障发生,随时待命接管主机工作
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: