`

mongoDB基础知识

 
阅读更多

启动服务:mongod.exe --dbpath D:\MongoDB\mongodbwin321.6.0\data
          --dbpath 数据文件存放路径
          --port 数据服务端口
启动客户端:mongo.exe cclove
            cclove  所连接的数据库名称

数据库操作语法:

  db.AddUser(username,password)  添加用户
  db.auth(usrename,password)     设置数据库连接验证
  db.cloneDataBase(fromhost)     从目标服务器克隆一个数据库

  db.copyDatabase(fromdb,todb,fromhost)  复制数据库fromdb---源数据库名称,todb---目标数据库名称,fromhost---源数据库服务器地址
  db.createCollection(name,{size:3333,capped:333,max:88888})  创建一个数据集,相当于一个表
  db.currentOp()                 取消当前库的当前操作
  db.dropDataBase()              删除当前数据库

  db.getCollection(cname)        取得一个数据集合,同用法:db['cname'] or db.cname
  db.getCollenctionNames()       取得所有数据集合的名称列表
  db.getLastError()              返回最后一个错误的提示消息
  db.getLastErrorObj()           返回最后一个错误的对象
  db.getMongo()                  取得当前服务器的连接对象get the server connection object

  db.getName()                   返回当操作数据库的名称
  db.getPrevError()              返回上一个错误对象

  db.killOp()                    停止在当前库的当前操作
  db.printCollectionStats()      返回当前库的数据集状态
  db.printReplicationInfo()
  db.printSlaveReplicationInfo()
  db.printShardingStatus()       返回当前数据库是否为共享数据库
  db.removeUser(username)        删除用户
  db.repairDatabase()            修复当前数据库

  db.setProfilingLevel(level)    0=off,1=slow,2=all
  db.shutdownServer()            关闭当前服务程序
  db.version()                   返回当前程序的版本信息
数据集(表)操作语法:
  db.haha.find({id:10})          返回haha数据集ID=10的数据集
  db.haha.find({id:10}).count()  返回haha数据集ID=10的数据总数
  db.haha.find({id:10}).limit(2) 返回haha数据集ID=10的数据集从第二条开始的数据集
  db.haha.find({id:10}).skip(8)  返回haha数据集ID=10的数据集从0到第八条的数据集
  db.haha.find({id:10}).limit(2).skip(8)  返回haha数据集ID=1=的数据集从第二条到第八条的数据
  db.haha.find({id:10}).sort()   返回haha数据集ID=10的排序数据集
  db.haha.findOne([query])       返回符合条件的一条数据
  db.haha.getDB()                返回此数据集所属的数据库名称
  db.haha.getIndexes()           返回些数据集的索引信息
  db.haha.group({key:...,initial:...,reduce:...[,cond:...]})
  db.haha.mapReduce(mayFunction,reduceFunction,<optional params>)
  db.haha.remove(query)                      在数据集中删除一条数据
  db.haha.renameCollection(newName)          重命名些数据集名称
  db.haha.save(obj)                          往数据集中插入一条数据
  db.haha.stats()                            返回此数据集的状态
  db.haha.storageSize()                      返回此数据集的存储大小
  db.haha.totalIndexSize()                   返回此数据集的索引文件大小
  db.haha.totalSize()                        返回些数据集的总大小
  db.haha.update(query,object[,upsert_bool]) 在此数据集中更新一条数据                        
  db.haha.validate()                         验证此数集                                       
  db.haha.getShardVersion()                  返回数据集共享版本号
  db.haha.find({'name':'foobar'})    select * from haha where name='foobar'
  db.haha.find()                     select * from haha
  db.haha.find({'ID':10}).count()    select count(*) from haha where ID=10
  db.haha.find().skip(10).limit(20)  从查询结果的第十条开始读20条数据  select * from haha limit 10,20  ----------mysql
  db.haha.find({'ID':{$in:[25,35,45]}})  select * from haha where ID in (25,35,45)
  db.haha.find().sort({'ID':-1})      select * from haha order by ID desc
  db.haha.distinct('name',{'ID':{$lt:20}})   select distinct(name) from haha where ID<20

  db.haha.group({key:{'name':true},cond:{'name':'foo'},reduce:function(obj,prev){prev.msum+=obj.marks;},initial:{msum:0}})
  select name,sum(marks) from haha group by name
  db.haha.find('this.ID<20',{name:1})     select name from haha where ID<20
  db.haha.insert({'name':'foobar','age':25})  insert into haha ('name','age') values('foobar',25)
  db.haha.insert({'name':'foobar','age':25,'email':'cclove2@163.com'})
  db.haha.remove({})                   delete * from haha
  db.haha.remove({'age':20})           delete haha where age=20
  db.haha.remove({'age':{$lt:20}})     delete haha where age<20
  db.haha.remove({'age':{$lte:20}})    delete haha where age<=20
  db.haha.remove({'age':{$gt:20}})     delete haha where age>20
  db.haha.remove({'age':{$gte:20}})    delete haha where age>=20
  db.haha.remove({'age':{$ne:20}})     delete haha where age!=20
  db.haha.update({'name':'foobar'},{$set:{'age':36}})  update haha set age=36 where name='foobar'
  db.haha.update({'name':'foobar'},{$inc:{'age':3}})   update haha set age=age+3 where name='foobar'

  查询colls所有数据:db.colls.find() //select * from colls
  通过指定条件查询: db.colls.find({'last_name': 'Smith'});//select * from colls where last_name='Smith'
  指定多条件查询:   db.colls.find( { x : 3, y : "foo" } );//select * from colls where x=3 and y='foo'
  指定条件范围查询: db.colls.find({j: {$ne: 3}, k: {$gt: 10} });//select * from colls where j!=3 and k>10
  查询不包括某内容: db.colls.find({}, {a:0});//查询除a为0外的所有数据
支持<, <=, >, >=查询,需用符号替代分别为$lt,$lte,$gt,$gte
  db.colls.find({ "field" : { $gt: value } } );  
  db.colls.find({ "field" : { $lt: value } } );  
  db.colls.find({ "field" : { $gte: value } } ); 
  db.colls.find({ "field" : { $lte: value } } );
  也可对某一字段做范围查询:db.colls.find({ "field" : { $gt: value1, $lt: value2 } } );
  不等于查询用字符$ne:     db.colls.find( { x : { $ne : 3 } } );
  in查询用字符$in:         db.colls.find( { "field" : { $in : array } } );
                            db.colls.find({j:{$in: [2,4,6]}});
  not in查询用字符$nin:    db.colls.find({j:{$nin: [2,4,6]}});
  取模查询用字符$mod:      db.colls.find( { a : { $mod : [ 10 , 1 ] } } )// where a % 10 == 1 
$all查询
db.colls.find( { a: { $all: [ 2, 3 ] } } );//指定a满足数组中任意值时
$size查询
db.colls.find( { a : { $size: 1 } } );//对对象的数量查询,此查询查询a的子对象数目为1的记录
$exists查询
db.colls.find( { a : { $exists : true } } ); // 存在a对象的数据
db.colls.find( { a : { $exists : false } } ); // 不存在a对象的数据
$type查询$type值为bsonhttp://bsonspec.org/数据的类型值
db.colls.find( { a : { $type : 2 } } ); // 匹配a为string类型数据
db.colls.find( { a : { $type : 16 } } ); // 匹配a为int类型数据
使用正则表达式匹配
db.colls.find( { name : /acme.*corp/i } );//类似于SQL中like
内嵌对象查询
db.colls.find( { "author.name" : "joe" } );
1.3.3版本及更高版本包含$not查询
db.colls.find( { name : { $not : /acme.*corp/i } } );
db.colls.find( { a : { $not : { $mod : [ 10 , 1 ] } } } );
sort()排序
db.colls.find().sort( { ts : -1 } );//1为升序2为降序
limit()对限制查询数据返回个数
db.colls.find().limit(10)
skip()跳过某些数据
db.colls.find().skip(10)
snapshot()快照保证没有重复数据返回或对象丢失
count()统计查询对象个数
db.students.find({'address.state' : 'CA'}).count();//效率较高
db.students.find({'address.state' : 'CA'}).toArray().length;//效率很低
group()对查询结果分组和SQL中group by函数类似 
distinct()返回不重复值

时间检索:

//检索 7月12 到 8月1号的数据

db.cpc_common.cpc_click_detail_log.count({date_created:{$gte:new Date(2010, 6,12), $lt:new Date(2010,7,1)}})

//删除 7月12 到 8月1号的数据

 db.cpc_common.cpc_click_detail_log.remove({date_created:{$gte:new Date(2010, 6,12), $lt:new Date(2010,7,1)}})

like 查询

query.put("search_keyword", Pattern.compile(search_word +"+"));

分享到:
评论

相关推荐

    Mongodb基础知识详解(值得珍藏).pdf

    掌握MongoDB基础知识对于开发人员来说具有重要的作用。MongoDB是一个流行的NoSQL数据库,它以高性能、可扩展性和灵活性而著称。在开发应用程序时,无论是Web应用程序、移动应用程序还是后端服务,都需要一个高效的...

    MongoDB交流-基础知识介绍

    mongodb基础知识介绍,架构, 实现

    mongodb_the_definitive_guide_2nd_edition.pdf

    第一部分展示MongoDB基础知识、核心概念。第二部分介绍使用MongoDB进行开发,包括索引的概念以及各种特殊索引和集合的用法等。第三部分讲述复制,包括副本集的相关概念、创建方法,与应用程序的交互等。第四部讨论分...

    python操作MongoDB基础知识

    MongoDB支持好多语言,今天我们就写一写python操作MongoDB的知识,从安装MongoDB到操作MongoDB全都有了。

    MongoDB权威指南 中文第2版-01卷

    一部分展示MongoDB基础知识、核心概念。二部分介绍使用MongoDB进行开发,包括索引的概念以及各种特殊索引和集合的用法等。三部分讲述复制,包括副本集的相关概念、创建方法,与应用程序的交互等。四部分讨论分片,...

    数据库mongodb

    mongodb基础知识,基础,基础基础基础基础基础基础基础基础基础基础基础基础基础基础基础基础基础基础基础基础基础基础基础基础基础基础基础基础

    mongodb-基础知识和高级知识

    mongodb_基础知识和高级知识

    MongoDB权威指南 中文第2版-02卷

    一部分展示MongoDB基础知识、核心概念。二部分介绍使用MongoDB进行开发,包括索引的概念以及各种特殊索引和集合的用法等。三部分讲述复制,包括副本集的相关概念、创建方法,与应用程序的交互等。四部分讨论分片,...

    MongoDB权威指南(第2版).pdf

    展示MongoDB基础知识、核心概念。第二部分介绍使用MongoDB进行开发,包括索引的概念以及各种特殊索引和集合的用法等。第三部分讲述复制,包括副本集的相关概念、创建方法,与应用程序的交互等。第四部讨论分片,包括...

    MongoDB:基本知识

    MongoDB Mongo Basic knowledge MongoDB 概览 mongo是一个高可用、分布式、面向集合的,模式自由的文档数据库。 mongo和传统关系数据库的最本质的区别在那里呢?就是其文档模型。 如何考虑MongoDB 文档模式设计的基本...

    MongoDB权威指南(第2版)中文版.pdf.part1

    一部分展示MongoDB基础知识、核心概念。二部分介绍使用MongoDB进行开发,包括索引的概念以及各种特殊索引和集合的用法等。三部分讲述复制,包括副本集的相关概念、创建方法,与应用程序的交互等。四部分讨论分片,...

    MongoDB权威指南(第2版)中文版.pdf.part2

    一部分展示MongoDB基础知识、核心概念。二部分介绍使用MongoDB进行开发,包括索引的概念以及各种特殊索引和集合的用法等。三部分讲述复制,包括副本集的相关概念、创建方法,与应用程序的交互等。四部分讨论分片,...

    mongodb 入门详解

    mongodb 基础知识入门详解:优,缺点,常用命令,数据基础操作解析

    Mongodb权威指南

    Monogodb新手文档,全面的mongodb基础知识和应用案例。

    MongoDB-Fundamentals:在现代云计算环境中学习实用的MongoDB技能

    MongoDB基础知识 这是发布的的存储库。 它包含从头到尾完成该课程所必需的所有支持项目文件。 要求和设置 要开始使用项目文件,您需要: 安装 安装 安装 关于MongoDB基础知识 将帮助您开始使用MongoDB在云计算...

    MongoDB使用手册

    介绍mongodb基础知识,工作原理,集群架构,以及使用java等相关操作

    MongoDB的基础知识简介

    主要介绍了MongoDB的基础知识简介,需要的朋友可以参考下

    MongoDB期末考试测试题

    通过阅读测试题并解答其中的问题,同学们可以加深对MongoDB的理解,夯实知识基础,并为期末考试做好准备。 其他说明:这份文档是为了帮助同学们复习MongoDB并测试他们的掌握程度而设计的。建议同学们在使用测试题时...

    MongoDB基础---知识积累

    NULL 博文链接:https://wuquanyin1011.iteye.com/blog/1199026

Global site tag (gtag.js) - Google Analytics