mongovue查询字段_mongodb查询速度

mongovue查询字段_mongodb查询速度{“ei”:”AW4BROILANDSTART1″,//条件一”cd”:{$elemMatch:{“0004″:{$gte:0}}}, //条件二,cd为集合,0004为集合中的key”st”:{$gte:ISODate(“2013-09-05T00:00:00.958Z”)}//时间条件,”im”:{$exists:true},”cn”:{$ne:””},

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
{“ei”:”AW4BROILANDSTART1″, //条件一


“cd”:{$elemMatch:{“0004”:{$gte:0}}},  //条件二,cd为集合 ,0004为集合中的key

“st”:{$gte:ISODate(“2013-09-05T00:00:00.958Z”)} //时间条件,

“im”:{$exists:true},

“cn”:{$ne:””},

{$and:[ {“cn”:{$ne:null}}, {“cn”:{$ne:””}}, 

{“sv”:”40″}, 

{“ltm”:{$gt:ISODate(“2014-03-17T16:00:00Z”)}}

]}

}

mysql 和 mongo db 语法对比

本文描述了MySQL中的常用SQL语句在MongoDB中的写法,如果你长期使用MySQL而对MongoDB跃跃欲试,这篇简单的文章可以帮助你更快的进入角色。
查询:
MySQL:
SELECT * FROM user
Mongo:
db.user.find()
MySQL:
SELECT * FROM user WHERE name = ’starlee’
Mongo:
db.user.find({‘name’ : ’starlee’})
插入:
MySQL:
INSERT INOT user (`name`, `age`) values (’starlee’,25)
Mongo:
db.user.insert({‘name’ : ’starlee’, ‘age’ : 25})
如果你想在MySQL里添加一个字段,你必须:
ALTER TABLE user….
但在MongoDB里你只需要:
db.user.insert({‘name’ : ’starlee’, ‘age’ : 25, ‘email’ : ’starlee@starlee.com’})
删除:
MySQL:
DELETE * FROM user
Mongo:
db.user.remove({})
MySQL:
DELETE FROM user WHERE age < 30
Mongo:
db.user.remove({‘age’ : {$lt : 30}})
$gt : > ; $gte : >= ; $lt : < ; $lte : <= ; $ne : !=
更新:
MySQL:
UPDATE user SET `age` = 36 WHERE `name` = ’starlee’
Mongo:
db.user.update({‘name’ : ’starlee’}, {$set : {‘age’ : 36}})
MySQL:
UPDATE user SET `age` = `age` + 3 WHERE `name` = ’starlee’
Mongo:
db.user.update({‘name’ : ’starlee’}, {$inc : {‘age’ : 3}})
MySQL:
SELECT COUNT(*) FROM user WHERE `name` = ’starlee’
Mongo:
db.user.find({‘name’ : ’starlee’}).count()
MySQL:
SELECT * FROM user limit 10,20
Mongo:
db.user.find().skip(10).limit(20)
MySQL:
SELECT * FROM user WHERE `age` IN (25, 35,45)
Mongo:
db.user.find({‘age’ : {$in : [25, 35, 45]}})
MySQL:
SELECT * FROM user ORDER BY age DESC
Mongo:
db.user.find().sort({‘age’ : -1})
MySQL:
SELECT DISTINCT(name) FROM user WHERE age > 20
Mongo:
db.user.distinct(‘name’, {‘age’: {$lt : 20}})
MySQL:
SELECT name, sum(marks) FROM user GROUP BY name
Mongo:
db.user.group({

key : {‘name’ : true},
cond: {‘name’ : ‘foo’},
reduce: function(obj,prev) { prev.msum += obj.marks; },
initial: {msum : 0}
});
MySQL:
SELECT name FROM user WHERE age < 20
Mongo:
db.user.find(‘this.age < 20′, {name : 1})
发现很多人在搜MongoDB循环插入数据,下面把MongoDB循环插入数据的方法添加在下面:
for(var i=0;i<100;i++)db.test.insert({uid:i,uname:’nosqlfan’+i});
上面一次性插入一百条数据,大概结构如下:
{ “_id” : ObjectId(“4c876e519e86023a30dde6b8″), “uid” : 55, “uname” : “nosqlfan55″ }
{ “_id” : ObjectId(“4c876e519e86023a30dde6b9″), “uid” : 56, “uname” : “nosqlfan56″ }
{ “_id” : ObjectId(“4c876e519e86023a30dde6ba”), “uid” : 57, “uname” : “nosqlfan57″ }
{ “_id” : ObjectId(“4c876e519e86023a30dde6bb”), “uid” : 58, “uname” : “nosqlfan58″ }
{ “_id” : ObjectId(“4c876e519e86023a30dde6bc”), “uid” : 59, “uname” : “nosqlfan59″ }
{ “_id” : ObjectId(“4c876e519e86023a30dde6bd”), “uid” : 60, “uname” : “nosqlfan60″ }
php 下推荐的mongo 客户端: http://code.google.com/p/rock-php/
mongo语法
http://wangjc-opal.javaeye.com/blog/802571
启动服务
mongod.exe –dbpath D:/MongoDB/mongodbwin321.6.0/data
–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,<optional params>)
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’
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/firetaker/archive/2010/08/12/5806628.aspx
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
MongoDB的好处挺多的,比如多列索引,查询时可以用一些统计函数,支持多条件查询,但是目前多表查询是不支持的,可以想办法通过数据冗余来解决多表 查询的问题。
MongoDB对数据的操作很丰富,下面做一些举例说明,内容大部分来自官方文档,另外有部分为自己理解。
查询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)}})

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/171638.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • php环境安装与配置_windows下php环境搭建

    php环境安装与配置_windows下php环境搭建配置时区打开php解压目录,找到php.ini-development文件,将其改名为php.ini,用记事本打开。找到(带分号);date.timezone=去掉前面的分好,修改为date.timezone=Asia/Shanghai测试:在根目录下的index.php文件夹中写入以下代码<?phpechodate(“Y:m:dH:i:s”…

    2022年9月22日
    4
  • java map 缓存_缓存用于

    java map 缓存_缓存用于缓存什么是缓存?平常的开发项目中,多多少少都会使用到缓存,因为一些数据我们没有必要每次查询的时候都去查询到数据库。缓存的使用场景:在Java应用中,对于访问频率高,更新少的数据,通常的方案是将这类数据加入缓存中,相对从数据库中读取,读缓存效率会有很大提升。在集群环境下,常用的分布式缓存有Redis等。但在某些业务场景上,可能不需要去搭建一套复杂的分布式缓存系统,在单机环境下,通常是会希望使用内部的缓存(LocalCache)。使用map缓存方案:基于ConcurrentHashMap实现数

    2022年9月27日
    4
  • Enterprise Library 4.0

    Enterprise Library 4.0微软发布了支持VisualStudio2008的新版本EnterpriseLibrary4.0,同时也发布了他们的依赖注入容器Unity应用程序块的1.1版本。模式与实践团队的产品经理GrigoriMelnik宣布发布EnterpriseLibrary4.0和Unity1.1更新,详细描述了所有新特性。MSDN开发中心的新闻稿解释了这个版本对开发人员的意义:此次发布…

    2022年10月20日
    3
  • Android Okio使用

    Android Okio使用Okio使用概述Okio是对JavaIO的封装,存储和处理数据变得更加容易。依赖库implementation’com.squareup.okio:okio:2.4.3’基本使用写操作try(BufferedSinksink=Okio.buffer(Okio.sink(newFile(“text.txt”)))){sink.writeInt(65);sink.writeUtf8(“hellookio”);sink.writeUtf8(“安卓”)

    2022年4月30日
    76
  • ZIlliqa团队关于分片、可扩展性和安全的智能合约的采访

    ZIlliqa团队关于分片、可扩展性和安全的智能合约的采访

    2021年7月1日
    66
  • 世界上公认最快的学习法 – 弗曼学习法

    世界上公认最快的学习法 – 弗曼学习法诺贝尔物理学奖得主-理查德·弗曼的学习方法,是世界上公认最快的学习方法,主要有四个步骤:1、选择一个你想要理解的知识;2、设想一下,你要向别人传授这个知识;3、如果过程中出现了问题,就重新回顾这个知识;4、让你的讲解越来越简单易懂。转载于:https://www.cnblogs.com/javalyy/p/10648980.html…

    2022年5月1日
    46

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号