java mongodb时间查询怎么查询某个时间的最后一条记录

MongoDB查询操作限制返回字段的方法
字体:[ ] 类型:转载 时间:
这篇文章主要介绍了MongoDB查询操作限制返回字段的方法,需要的朋友可以参考下
映射(projection )声明用来限制所有查询匹配文档的返回字段。projection以文档的形式列举结果集中要包含或者排除的字段。可以指定要包含的字段(例如:{field:1})或者指定要排除的字段(例如:{field:0})。默认_id是包含在结果集合中的,要从结果集中排除_id字段,需要在projection中指定排除_id字段({_id:0})。除了_id字段,不能在一个projection中联合使用包含和排除语意。返回匹配文档的所有字段:如果没有指定projection,find()方法返回所有匹配文档的所有字段。 代码如下:db.inventory.find( { type: 'food' } )这个例子将返回inventory集合中type字段的值为"food"的所有文档,返回的文档包含全部字段。返回指定字段和_id字段:一个projection可以明确地指定多个字段。下面的操作中,find()方法返回匹配的所有文档。在结果集中,只有item和qty字段,默认_id字段也是返回的。 代码如下:db.inventory.find( { type: 'food' }, { item: 1, qty: 1 } )仅返回指定字段:可以通过在projection中指定排除_id字段将其从结果中去掉,如下例子所示: 代码如下:db.inventory.find( { type: 'food' }, { item: 1, qty: 1, _id:0 } )返回除排除掉以外的字段:可以使用一个projection排除一个或者一组字段,如下: 代码如下:db.inventory.find( { type: 'food' }, { type:0 } )这个操作返回所有type字段值为food的文档,在结果中type字段不返回。数组字段的projection:&$elemMatch 和 $slice运算符是对数组进行projection的唯一途径。
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具13:05 提问
MongoDB查询时间范围内的数据
ref.put("clock_time", new BasicDBObject("$lte"," 17:57:55"));
ref.put("clock_time", new BasicDBObject("$gte"," 17:57:51"));
用这种方法不行啊
这两句只有一句话起作用,哪句在前哪句起作用,不能两边的范围都卡住
见鬼了 - -
按赞数排序
你键值一样,覆盖了。
哎呀 知道了 的确是覆盖了
java 操作mongodb 查询2个时间段的数据怎么弄??? 求大神
Mongo mongo = new Mongo("172.18.17.30", 27017);
DB db = mongo.getDB("sina_sport");
DBCollection coll = db.getCollection("tongjis_nba");
BasicDBObject query = new BasicDBObject();
query.put("cdt", new BasicDBObject("$gte",
"T00:00:00.000Z"));
int iCount = coll.find(query).count();
System.out.println(iCount);
哥们,您这个问题怎么解决了,我也遇到了类似的问题,您帮忙看看问题出在哪里?
for(Document doc : collection.find(and(gte("releaseDate", new Date(, 15, 06, 40)), lte("releaseDate", new Date(, 15, 07, 10))))) {
System.out.println("name:" + doc.getString("name") + "\t"
+ "price:" + doc.getInteger("price") + "\t"
+ "version:" + doc.getString("version") + "\t"
+ "releaseDate:" + doc.getDate("releaseDate"));
其他相似问题温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
francs推荐阅读:
阅读(46236)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
在LOFTER的更多文章
loftPermalink:'',
id:'fks_',
blogTitle:'MongoDB:删除记录( Delete )',
blogAbstract:'
&&&&&&&&&& &前面介绍了 MongoDB 的查询,插入,更新操作,接下来介绍 Delete 操作。&&&
一. 根据查询条件删除文档--1.1 查询 id=1 的所有文档
&& db.test_2.find({id:1});{ \"_id\" : ObjectId(\"50a0d46ceb825d827b0c3f9b\"), \"id\" : 1, \"name\" : \"name_2\" }{ \"_id\" : ObjectId(\"50a12e34f73f2e4aa1ff71bc\"), \"id\" : 1, \"name\" : \"francs\" }& &&&
--1.2 删除 id=1 的所有文档
blogTag:'',
blogUrl:'blog/static/',
isPublished:1,
istop:false,
modifyTime:2,
publishTime:7,
permalink:'blog/static/',
commentCount:0,
mainCommentCount:0,
recommendCount:1,
bsrk:-100,
publisherId:0,
recomBlogHome:false,
currentRecomBlog:false,
attachmentsFileIds:[],
groupInfo:{},
friendstatus:'none',
followstatus:'unFollow',
pubSucc:'',
visitorProvince:'',
visitorCity:'',
visitorNewUser:false,
postAddInfo:{},
mset:'000',
remindgoodnightblog:false,
isBlackVisitor:false,
isShowYodaoAd:true,
hostIntro:'',
hmcon:'1',
selfRecomBlogCount:'0',
lofter_single:''
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}在本章中,我们将学习如何使用MongoDB中的限制记录数量。
MongoDB limit()方法要限制 MongoDB 中返回的记录数,需要使用limit()方法。 该方法接受一个数字类型参数,它是要显示的文档数。
limit()方法的基本语法如下:
& db.COLLECTION_NAME.find().limit(NUMBER)
假设集合myycol有以下数据。
& db.mycol.find({},{'_id':1, 'title':1})
{ &_id& : 101, &title& : &MongoDB Guide& }
{ &_id& : 102, &title& : &NoSQL Database& }
{ &_id& : 104, &title& : &Python Quick Guide& }
{ &_id& : 100, &title& : &MongoDB Overview& }
以下示例将在查询文档时仅显示两个文档。
& db.mycol.find({},{&title&:1,_id:0}).limit(2)
{ &title& : &MongoDB Guide& }
{ &title& : &NoSQL Database& }
如果没有在limit()方法中指定number参数的值,那么它将显示集合中的所有文档。
MongoDB skip()方法除了limit()方法之外,还有一个方法skip()也接受数字类型参数,用于跳过文档数量。
skip()方法的基本语法如下 -
&db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)
以下示例将仅显示第三个文档。
& db.mycol.find({},{&title&:1,_id:0}).limit(1).skip(2)
{ &title& : &Python Quick Guide& }
请注意,skip()方法中的默认值为0。
本站所有代码下载:请扫描本页面底部(右侧)二维码并关注微信公众号,回复:"代码下载" 获取。
上一篇:下一篇:查看: 9038|回复: 1
MongoDB命令及性能查看
认证徽章论坛徽章:5
& &&&此内容主要来自:,其间也Google了很多文章,在此只是留下笔记。下面的内容只是对不易懂的进行了简要的说明,容易懂的就没有进行说明。
帮助命令查看
&&help& && && && && && && && & show help&&show dbs& && && && && && && &show database names&&show collections& && && && & show collections in current&&database&&show users& && && && && && & show users in current&&database&&show profile& && && && && &&&show most recent system.profile&&entries with time &= 1ms&&use &db name&& && && && && & set curent database to &db&&name&&&db.addUser (username, password)&&db.removeUser(username)&&db.cloneDatabase(fromhost)&&db.copyDatabase(fromdb, todb, fromhost)&&db.createCollection(name, { size : ...,&&capped : ..., max : ... } )&&db.getName()&&db.dropDatabase()&&// runs the collstats] command on each&&collection in the database&&db.printCollectionStats()&&db.currentOp() displays the current&&operation in the db&&db.killOp() kills the current operation&&in the db&&db.getProfilingLevel()&&db.setProfilingLevel(level) 0=off 1=slow&&2=all&&db.getReplicationInfo()&&db.printReplicationInfo()&&db.printSlaveReplicationInfo()&&db.repairDatabase()&&db.version() current version of the&&server&&db.commandHelp(&collStats&)&&db.shutdownServer()&&
& && &&&#######################collections(集合帮帮助)###########################
&&db.foo.drop() drop the collection&&db.foo.dropIndex(name)&&db.foo.dropIndexes()&&db.foo.getIndexes()&&db.foo.ensureIndex(keypattern,options) -&&options object has these possible&&& && && && && && && && && && && && && && &fields: name, unique, dropDups&&db.foo.find( [query] , [fields])& && & - first parameter is an optional&&& && && && && && && && && && && && && && &query filter. second parameter&&& && && && && && && && && && && && && &&&is optional&&& && && && && && && && && && && && && &&&set&&of fields to return.&&& && && && && && && && && && && && && && &&&e.g.&&db.foo.find(&&& && && && && && && && && && && && && && && && && &{ x : 77 } ,&&& && && && && && && && && && && && && && && && && &{ name : 1 , x : 1 } )&&db.foo.find(...).count()&&db.foo.find(...).limit(n)&&db.foo.find(...).skip(n)&&db.foo.find(...).sort(...)&&db.foo.findOne([query])&&db.foo.getDB() get DB object associated&&with collection&&db.foo.count()&&db.foo.group( { key : ..., initial: ...,&&reduce : ...[, cond: ...] } )&&db.foo.renameCollection( newName )&&renames the collection&&db.foo.stats()&&db.foo.dataSize()&&db.foo.storageSize() - includes free&&space allocated to this collection&&db.foo.totalIndexSize() - size in bytes&&of all the indexes&&db.foo.totalSize() - storage allocated&&for all data and indexes&&db.foo.validate() (slow)&&db.foo.insert(obj)&&db.foo.update(query, object[,&&upsert_bool])&&db.foo.save(obj)&&db.foo.remove(query)& && && && && && & - remove objects matching query&&& && && && && && && && && && && && && && &remove({}) will remove all&&
& &&&#######################&&replset command###############&&rs.help()&&& && && &rs.status()& && && && && && && &{ replSetGetStatus : 1 }&&checks repl set status&&& && && &rs.initiate()& && && && && && & { replSetInitiate : null }&&initiates set with default settings&&& && && &rs.initiate(cfg)& && && && && & { replSetInitiate : cfg }&&initiates set with configuration cfg&&& && && &rs.conf()& && && && && && && &&&get the current&&configuration object from & & local.system.replset&&& && && &rs.reconfig(cfg)& && && && && & updates the configuration of&&a running replica set with & & cfg (disconnects)&&& && && &rs.add(hostportstr)& && && && & add a new member to the set with&&default attributes & & (disconnects)&&& && && &rs.add(membercfgobj)& && && && &add a new member to the set with&&extra attributes & & (disconnects)&&& && && &rs.addArb(hostportstr)& && && & add a new member which is&&arbiterOnly:true & & (disconnects)&&& && && &rs.stepDown([secs])& && && && & step down as primary&&(momentarily) (disconnects)&&& && && &rs.freeze(secs)& && && && && &&&make a node ineligible to&&become primary for the time & & specified&&& && && &rs.remove(hostportstr)& && && & remove a host from the replica set&&(disconnects)&&& && && &rs.slaveOk()& && && && && && &&&shorthand for&&db.getMongo().setSlaveOk()&&
&&& && && &db.isMaster()& && && && && && & check who is primary&&&&
& &&&如果忘记了,可以用以下的命令查看:
&&help&&db.help();&&db.yourColl.help();&&db.youColl.find().help();&&rs.help();
1、&&进入数据库:& && & useadmin2、&&查看用户列表db.system.users.find()3、&&增加用记或修改用户密码& &db.addUser(‘name’,’pwd’,true)& && & 其中true是否是只读4、&&用户认证& &db.auth(‘name’,’pwd’,true)& &&&其中true是否是只读,当显示为1时表示成功。5、&&删除用户db.removeUser(‘name’)6、&&查看所有用户:& & show users7、&&查看所有数据库show dbs8、&&查看所有的collection的状态show collections9、& & 查看各collection的状态db.printCollectionsStats()& & 10、查看主从复制状态& && && &&&db.printReplicationInfo()& & 11、修复数据库& && && && && && && &&&db.repairDatabase()& & 12、设置记录profiling,0=off 1=slow 2=all& && && && && && && && && & db.setProfilingLevel(1)& & 13、查看profiling& && && && && && && &showprofile& & 14、&&拷贝数据库& && && && && && && &db.copyDatabase('mail_addr','mail_addr_tmp')& & 15、&&删除collection& && && && && && && &db.mail_addr.drop()& & 16、删除当前的数据库& && && && && && && &db.dropDatabase()17、查看同步情况状态& && & db.printReplicationInfo();18、手动同步:& && & use admin& && & db.runCommand ( {&resync&: 1 } )19、关闭数据库& && & db.shutdownServer()& & 当出现:Error:couldn't connect to server 127.0.0.1 shell/mongo.js:84& && &(1)、rm/mongodb/data/mongo.lock& && &(2)、mongod--repair& && &(3)、重启20、从指定主机上克隆数据库db.cloneDatabase(“127.0.0.1”);& & 21、从指定的机器上复制指定数据库数据到某个数据库& && && & db.copyDatabase(&mydb&, &temp&,&127.0.0.1&);& && && && &&&将本机的mydb的数据复制到temp数据库中& & 22、删除当前使用数据库& && && & db.dropDatabase();23、查看当前db的链接机器地址& && &db.getMongo();& && && && && & 三、增加删除修改1、Insertdb.user.insert({'name':'dump','age':1})ordb.user.save({'name':'dump','age':1})& && && &嵌套对象:& && && &db.foo.save({'name':'dump','address':{'city':'hangzhou','post':310015},'phone':[13& && && &&&数组对象:db.user_addr.save({'Uid':'dump','Al':['test-','test-']})& &&&2、delete& && && &删除name=’dump’的用户信息:& && && && && & db.user.remove({'name':'dump'})& && && &&&删除foo表所有信息:& && && && && & db.foo.remove()& && &&&3、update&&db.wkgbc.update({&id&:10},{&$set&:{&name&:&Gavin&,&value1&:110}})只更新第一条&&db.wkgbc.update({&id&:10},{&$set&:{&name&:&Gavin&,&value1&:110}},false,true)&&全部更新&&db.wkgbc.update({&id&:10},{&$set&:{&name&:&Gavin&,&value1&:110}},true,true)&&全部更新,且如果更新的字段不存在,则加入。&&db.wkgbc.update({&id&:11},{&$inc&:{&value5&:120}})&&其中inc是加入一个新的字段&&db.wkgbc.update({&id&:11},{&$unset&:{&value5&:110}})&&删除字段&& & && && && && && &四、其它查询:coll.find() // select * from collcoll.find().limit(10) // select * from coll limit 10coll.find().sort({x:1}) // select * from coll order by x asc 升序 -1降序coll.find().sort({x:1}).skip(5).limit(10) // select * from coll order by x asc limit 5, 10coll.find({x:10}) // select * from coll where x = 10coll.find({x: {$lt:10}}) // select * from coll where x &= 10coll.find({}, {y:true}) // select y from collcoll.count() //select count(*) from colldb.wkgbc.find({id:{$mod:[10,1]}})& &查询1-10外的数,(其中1会显示出来)& &db.wkgbc.find({id:{$ne:3}})& &&&除3外,显示所有数据db.wkgbc.find({id:{$in:[3]}})& && &显示等于3的数据db.wkgbc.find( { $and: [ { id: 10 }, { id : { $gt: 5 } } ] } ) 显示id大于5且id等于10的db.wkgbc.find({&UserName&:/keyword/})&&查看关键字db.wkgbc.find({&UserName&:/^keyword/})&&查看开头关键字db.wkgbc.find({&UserName&:/keyword$/})& &查看结尾关键字db.wkgbc.find().count()& && && && && &&&统计条数db.wkgbc.find({&$or&:[{&id&:10},{&value1&:/00/}]},{&id&:true}) 查找id=0的或value1有00的关键字的数据,其中id:true是只显示这两个字段,去掉会显示所有字段的数据
其他:wkgbc.find({&address.city&:&gz&}) // 搜索嵌套文档address中city值为gz的记录wkgbc.find({likes:&math&}) // 搜索数组wkgbc.find({name: {$exists: true}}); //查询所有存在name字段的记录wkgbc.find({phone: {$exists: false}}); //查询所有不存在phone字段的记录wkgbc.find({name: {$type: 2}}); //查询所有name字段是字符类型的wkgbc.find({age:{$type:
索引:wkgbc.ensureIndex({productid:1}) // 在productid上建立普通索引wkgbc.ensureIndex({district:1, plate:1}) // 多字段索引wkgbc.ensureIndex({&address.city&:1}) // 在嵌套文档的字段上建索引wkgbc.ensureIndex({productid:1}, {unique:true}) // 唯一索引wkgbc.ensureIndex({productid:1}, {unique:true, dropDups:true|) // 建索引时,如果遇到索引字段值已经出现过的情况,则删除重复记录wkgbc.getIndexes() // 查看索引wkgbc.dropIndex({productid:1}) // 删除单个索引
Mongostat性能健康查看
db.stats()&&PRIMARY& db.stats()&&{&&& && && &&db&&&: &admin&,& && && &当前数据库&&& && && &&wkgbcections&&&: 0,& && &&&集合数量&&& && && &&objects&&&: 0,& && && &&&&&& && && &&avgObjSize&&&: 0,& && && &平均obj大小&&& && && &&dataSize&&&: 0,& && && &&&数据库总大小&&& && && &&storageSize&&&: 0,& && && &储存大小&&& && && &&numExtents&&&: 0,& && & &&& && && &&indexes&&&: 0,& && && & 索引数&&& && && &&indexSize&&&: 0,& && && &索引大小&&& && && &&fileSize&&&: 0,& && && &&&文件大小&&& && && &&nsSizeMB&&&: 0,& && && &&&& && && &&ok&&&: 1&&}&&
db.serverStatus()
&&PRIMARY& db.serverStatus()&&{&&& && && &&host&&&: &localhost.localdomain&,&&& && && &&version&&&: &2.0.6&,&&& && && &&process&&&: &mongod&,&&& && && &&uptime&&&: 8345,&&& && && &&uptimeEstimate&&&: 7979,&&& && && &&localTime&&&: ISODate(&T14:46:51.890Z&),&&& && && &&globalLock&&&: {&&& && && && && && & &totalTime&&&: ,&&& && && && && && & &lockTime&&&: ,&&& && && && && && & &ratio&&&: 0.239012,&&& && && && && && & &currentQueue&&&: {& && & 当前查询队列&&& && && && && && && && && & &total&&&: 0,&&& && && && && && && && && & &readers&&&: 0,&&& && && && && && && && && & &writers&&&: 0&&& && && && && && & },&&& && && && && && & &activeClients&&&: {& && && &客户端活动情况&&& && && && && && && && && & &total&&&: 2,&&& && && && && && && && && & &readers&&&: 2,&&& && && && && && && && && & &writers&&&: 0&&& && && && && && & }&&& && && &},&&& && && &&mem&&&: {& && && && && && & 内存情况&&& && && && && && & &bits&&&: 64,& && && && &&&系统是64位&&& && && && && && & &resident&&&: 177,&&& && && && && && & &virtual&&&: 3514,& & journaling开启时,该值是mapped的2倍。如果比mapped小,说明出现内在溢出&&& && && && && && & &supported&&&: true,&&& && && && && && & &mapped&&&: 1600,&&& && && && && && & &mappedWithJournal&&&: 3200&&& && && &},&&& && && &&connections&&&: {& && && && & 连接数&&& && && && && && & &current&&&: 6,&&& && && && && && & &available&&&: 813& && && &还有可用&&& && && &},&&& && && &&extra_info&&&: {& && && &&&显示 一此额外系统信息&&& && && && && && & &note&&&: &fields vary by platform&,&&& && && && && && & &heap_usage_bytes&&&: 717504,&&& && && && && && & &page_faults&&&: 2042&&& && && &},&&& && && &&indexCounters&&&: {& && && & 索引情况&&& && && && && && & &btree&&&: {&&& && && && && && && && && & &accesses&&&: 3,&&& && && && && && && && && & &hits&&&: 3,& &&&& && && && && && && && && & &misses&&&: 0,&&& && && && && && && && && & &resets&&&: 0,& & 自数据库重启到今,索引重置了 几次&&& && && && && && && && && & &missRatio&&&: 0& & 用misses除accesses的比率,命中率&&& && && && && && & }&&& && && &},&&& && && &&backgroundFlushing&&&: {& && & 写入磁盘情况&&& && && && && && & &flushes&&&: 139,&&& && && && && && & &total_ms&&&: 1872,&&& && && && && && & &average_ms&&&: 13.575,&&& && && && && && & &last_ms&&&: 1,&&& && && && && && & &last_finished&&&: ISODate(&T14:46:46.836Z&)&&& && && &},&&& && && &&cursors&&&: {& && && && &cursor使用信息和情况&&& && && && && && & &totalOpen&&&: 2,&&& && && && && && & &clientCursors_size&&&: 2,&&& && && && && && & &timedOut&&&: 0&&& && && &},&&& && && &&network&&&: {& && && && &网络流量&&& && && && && && & &bytesIn&&&: ,&&& && && && && && & &bytesOut&&&: ,&&& && && && && && & &numRequests&&&: 13333&&& && && &},&&& && && &&repl&&&: {& && && && && &副本集群情况&&& && && && && && & &setName&&&: &myset&,&&& && && && && && & &ismaster&&&: true,&&& && && && && && & &secondary&&&: false,&&& && && && && && & &hosts&&&: [& && && &&&主机&&& && && && && && && && && & &192.168.29.130:27017&,&&& && && && && && && && && & &192.168.29.128:27017&,&&& && && && && && && && && & &192.168.29.129:27017&&&& && && && && && & ],&&& && && && && && & &primary&&&: &192.168.29.130:27017&,&&& && && && && && & &me&&&: &192.168.29.130:27017&&&& && && &},&&& && && &&opcounters&&&: {& && && & 操作情况&&& && && && && && & &insert&&&: 155,&&& && && && && && & &query&&&: 9,&&& && && && && && & &update&&&: 0,&&& && && && && && & &delete&&&: 0,&&& && && && && && & &getmore&&&: 4777,&&& && && && && && & &command&&&: 8397&&& && && &},&&& && && &&asserts&&&: {& && && &报出来的错误信息& &&&&&& && && && && && & &regular&&&: 0,&&& && && && && && & &warning&&&: 0,&&& && && && && && & &msg&&&: 0,&&& && && && && && & &user&&&: 0,&&& && && && && && & &rollovers&&&: 0&&& && && &},&&& && && &&writeBacksQueued&&&: false,& && & MongoDB写入磁盘情况&&& && && &&dur&&&: {& && && && & Journaling信息&&& && && && && && & &commits&&&: 29,& & 在上一次时间间隔&&& && && && && && & &journaledMB&&&: 0,& &&&journale的大小&&& && && && && && & &writeToDataFilesMB&&&: 0,&&& && && && && && & &compression&&&: 0,&&& && && && && && & &commitsInWriteLock&&&: 0,&&& && && && && && & &earlyCommits&&&: 0,&&& && && && && && & &timeMs&&&: {& && && & 实例操作性能的时间&&& && && && && && && && && & &dt&&&: 3103,&&& && && && && && && && && & &prepLogBuffer&&&: 0,&&& && && && && && && && && & &writeToJournal&&&: 0,&&& && && && && && && && && & &writeToDataFiles&&&: 0,&&& && && && && && && && && & &remapPrivateView&&&: 0&&& && && && && && & }&&& && && &},&&& && && &&ok&&&: 1&&}&&
db.currentOp()
& &在一台有负载的机器上:currentOp()
在一台没有负载的机器上:
如果发现一个操作太长,把数据库卡死的话,可以用这个命令杀死他&db.killOp(&shard3:&)
rs.status()&&SECONDARY& rs.status();&&{&&& && && &&set&&&: &myset&,& && && & replset的名字&&& && && &&date&&&: ISODate(&T06:13:35Z&),& && & &&& && && &&myState&&&: 2,&&& && && &&syncingTo&&&: &192.168.101.132:27017&,&&& && && &&members&&&: [&&& && && && && && & {&&& && && && && && && && && & &_id&&&: 3,& && && && &&&成员号&&& && && && && && && && && & &name&&&: &192.168.101.131:27017&,&&& && && && && && && && && & &health&&&: 1,& && && & 其中0表示服务down;1表示良好&&& && && && && && && && && & &state& : 2,& && && &&&&&& && && && && && && && && & &stateStr&&&: &SECONDARY&,& &&&表示是primary还是secondary&&& && && && && && && && && & &optime&&&: {&&& && && && && && && && && && && && & &t&&&: 0,&&& && && && && && && && && && && && & &i&&&: 2&&& && && && && && && && && & },&&& && && && && && && && && & &optimeDate&&&: ISODate(&T06:12:06Z&),&&& && && && && && && && && & &self&&&: true&&& && && && && && & },&&& && && && && && &{&&& && && && && && && && && & &_id&&&: 4,&&& && && && && && && && && & &name&&&: &192.168.201.131:27017&,&&& && && && && && && && && & &health&&&: 0,&&& && && && && && && && && & &state&&&: 8,&&& && && && && && && && && & &stateStr&&&: &(not reachable/healthy)&,&&& && && && && && && && && & &uptime&&&: 0,&&& && && && && && && && && & &optime&&&: {&&& && && && && && && && && && && && & &t&&&: 0,&&& && && && && && && && && && && && & &i&&&: 4&&& && && && && && && && && & },&&& && && && && && && && && & &optimeDate&&&: ISODate(&T18:34:57Z&),&&& && && && && && && && && & &lastHeartbeat&&&: ISODate(&T18:52:47Z&),&&& && && && && && && && && & &pingMs&&&: 0,& &&&表示这个节点到另一个节点发出的心跳命令时长&&& && && && && && && && && & &errmsg&&&: &socket exception&&&& && && && && && & },&&& & 其中state的解释:& & & && &0& && &
Starting up, phase 1 (parsing& && &configuration)
& && &1& && &
& && &2& && &
& && &3& && &
Recovering (initial syncing, post-rollback,& && &stale members)
& && &4& && &
Fatal error
& && &5& && &
Starting up, phase 2 (forking threads)
& && &6& && &
Unknown state (member has never been& && &reached)
& && &7& && &
& && &8& && &
& && &9& && &
& && &10& && &
& && &&&& & mongostat详解mongostat是mongdb自带的状态检测工具,在命令行下使用。它会间隔固定时间获取mongodb的当前运行状态,并输出。如果你发现数据库突然变慢或者有其他问题的话,你第一手的操作就考虑采用mongostat来查看mongo的状态。它的输出有以下几列:·& && &&&inserts/s 每秒插入次数·& && &&&query/s 每秒查询次数·& && &&&update/s 每秒更新次数·& && &&&delete/s 每秒删除次数·& && &&&getmore/s 每秒执行getmore次数·& && &&&command/s 每秒的命令数,比以上插入、查找、更新、删除的综合还多,还统计了别的命令·& && &&&flushs/s 每秒执行fsync将数据写入硬盘的次数。·& && &&&mapped/s 所有的被mmap的数据量,单位是MB,·& && &&&vsize 虚拟内存使用量,单位MB·& && &&&res 物理内存使用量,单位MB·& && &&&faults/s 每秒访问失败数(只有Linux有),数据被交换出物理内存,放到swap。不要超过100,否则就是机器内存太小,造成频繁swap写入。此时要升级内存或者扩展·& && &&&locked % 被锁的时间百分比,尽量控制在50%以下吧·& && &&&idx miss % 索引不命中所占百分比。如果太高的话就要考虑索引是不是少了·& && &&&q t|r|w 当Mongodb接收到太多的命令而数据库被锁住无法执行完成,它会将命令加入队列。这一栏显示了总共、读、写3个队列的长度,都为0的话表示mongo毫无压力。高并发时,一般队列值会升高。·& && &&&conn 当前连接数·& && &&&time 时间戳2、使用profiler& & Profiler默认是关闭的, MongoDB可以监控所有慢的以及不慢的查询。慢查询的时候开启。&&& use test&&switched to db test&&&&&db.setProfilingLevel(2); 设定级别&&{&was& : 0 , &slowms& : 100, &ok& : 1} // &was& is the old&&setting&&& db.getProfilingLevel()& &查看级别&&2&&查看Profile日志:&&&&&db.system.profile.find().sort({$natural:-1})&&{&ts& : &Thu Jan 29 :32&&GMT-0500 (EST)& , &info& :&&&query test.$cmd ntoreturn:1 reslen:66 nscanned:0&&query: { profile: 2 } nreturned:1&&bytes:50& ,&&&millis& : 0} ...&&·& && &&&字段意思·& && &&&ts:时间戳·& && &&&info:具体的操作·& && &&&millis:操作所花时间,毫秒 官网解释:使用Web控制台的端口在Mongodb数据库服务器端口的基础上加1000,如果是默认的Mongodb数据服务端口(Which is 27017),则相应的Web端口为28017这个页面可以看到·& && &&&当前Mongodb的所有连接·& && &&&各个数据库和Wkgbcection的访问统计,包括:Reads, Writes,Queries, GetMores ,Inserts, Updates, Removes·& && &&&写锁的状态·& && &&&以及日志文件的最后几百行(CentOS+10gen yum安装的mongodb默认的日志文件位于/var/log/mongo/mongod.log)l&&其中web查看的数据相当于rs.status()显示的数据。
认证徽章论坛徽章:277
总觉得不错。
itpub.net All Right Reserved. 北京皓辰网域网络信息技术有限公司版权所有    
 北京市公安局海淀分局网监中心备案编号: 广播电视节目制作经营许可证:编号(京)字第1149号

我要回帖

更多关于 mongodb时间段查询 的文章

 

随机推荐