热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

MongoDB下的高级查询示例

[root@localhost~]#mongoMongoDBshellversion:1.8.1connectingto:testgt;dbtestgt;showcollections

[root@localhost ~]# mongo MongoDB shell version: 1.8.1 connecting to: test gt; db test gt; show collections

[root@localhost ~]# mongo
MongoDB shell version: 1.8.1
connecting to: test
> db
test
> show collections
data_test
system.indexes
system.users
> db.data_test.find().skip(3).limit(4)//分页查询,从第4条记录起,每页4条。
{ "_id" : ObjectId("4dd7c914b2d5f68db79cdf57"), "userId" : "10010174", "userName" : "Bill Tu4", "gender" : "m4", "interests" : { "game" : "game4", "ball" : "ball4",
"other" : "nothing4" } }
{ "_id" : ObjectId("4dd7c914b2d5f68db79cdf58"), "userId" : "10010175", "userName" : "Bill Tu5", "gender" : "m5", "interests" : { "game" : "game5", "ball" : "ball5",
"other" : "nothing5" } }
{ "_id" : ObjectId("4dd7c914b2d5f68db79cdf59"), "userId" : "10010176", "userName" : "Bill Tu6", "gender" : "m6", "interests" : { "game" : "game6", "ball" : "ball6",
"other" : "nothing6" } }
{ "_id" : ObjectId("4dd7c914b2d5f68db79cdf5a"), "userId" : "10010177", "userName" : "Bill Tu7", "gender" : "m7", "interests" : { "game" : "game7", "ball" : "ball7",
"other" : "nothing7" } }
> db.data_test.find({},{},4,3)//与上相同,注意此页大小和起始位置的位置
{ "_id" : ObjectId("4dd7c914b2d5f68db79cdf57"), "userId" : "10010174", "userName" : "Bill Tu4", "gender" : "m4", "interests" : { "game" : "game4", "ball" : "ball4",
"other" : "nothing4" } }
{ "_id" : ObjectId("4dd7c914b2d5f68db79cdf58"), "userId" : "10010175", "userName" : "Bill Tu5", "gender" : "m5", "interests" : { "game" : "game5", "ball" : "ball5",
"other" : "nothing5" } }
{ "_id" : ObjectId("4dd7c914b2d5f68db79cdf59"), "userId" : "10010176", "userName" : "Bill Tu6", "gender" : "m6", "interests" : { "game" : "game6", "ball" : "ball6",
"other" : "nothing6" } }
{ "_id" : ObjectId("4dd7c914b2d5f68db79cdf5a"), "userId" : "10010177", "userName" : "Bill Tu7", "gender" : "m7", "interests" : { "game" : "game7", "ball" : "ball7",
"other" : "nothing7" } }
> db.data_test.find().sort({"userName":-1})//order by:按userName倒序
{ "_id" : ObjectId("4dd7c914b2d5f68db79cdf5c"), "userId" : "10010179", "userName" : "Bill Tu9", "gender" : "m9", "interests" : { "game" : "game9", "ball" : "ball9",
"other" : "nothing9" } }
{ "_id" : ObjectId("4dd7c914b2d5f68db79cdf5b"), "userId" : "10010178", "userName" : "Bill Tu8", "gender" : "m8", "interests" : { "game" : "game8", "ball" : "ball8",
"other" : "nothing8" } }
{ "_id" : ObjectId("4dd7c914b2d5f68db79cdf5a"), "userId" : "10010177", "userName" : "Bill Tu7", "gender" : "m7", "interests" : { "game" : "game7", "ball" : "ball7",
"other" : "nothing7" } }
{ "_id" : ObjectId("4dd7c914b2d5f68db79cdf59"), "userId" : "10010176", "userName" : "Bill Tu6", "gender" : "m6", "interests" : { "game" : "game6", "ball" : "ball6",
"other" : "nothing6" } }
{ "_id" : ObjectId("4dd7c914b2d5f68db79cdf58"), "userId" : "10010175", "userName" : "Bill Tu5", "gender" : "m5", "interests" : { "game" : "game5", "ball" : "ball5",
"other" : "nothing5" } }
{ "_id" : ObjectId("4dd7c914b2d5f68db79cdf57"), "userId" : "10010174", "userName" : "Bill Tu4", "gender" : "m4", "interests" : { "game" : "game4", "ball" : "ball4",
"other" : "nothing4" } }
{ "_id" : ObjectId("4dd7c914b2d5f68db79cdf56"), "userId" : "10010173", "userName" : "Bill Tu3", "gender" : "m3", "interests" : { "game" : "game3", "ball" : "ball3",
"other" : "nothing3" } }
{ "_id" : ObjectId("4dd7c914b2d5f68db79cdf55"), "userId" : "10010172", "userName" : "Bill Tu2", "gender" : "m2", "interests" : { "game" : "game2", "ball" : "ball2",
"other" : "nothing2" } }
{ "_id" : ObjectId("4dd7c914b2d5f68db79cdf5d"), "userId" : "100101710", "userName" : "Bill Tu10", "gender" : "m10", "interests" : { "game" : "game10", "ball" :
"ball10", "other" : "nothing10" } }
{ "_id" : ObjectId("4dd7c914b2d5f68db79cdf54"), "userId" : "10010171", "userName" : "Bill Tu1", "gender" : "m1", "interests" : { "game" : "game1", "ball" : "ball1",
"other" : "nothing1" } }
> db.data_test.find({"userName":{$ne:"Bill Tu10"},"age":{$gt:7}})//查询userName!='Bill Tu10' and age>7
{ "_id" : ObjectId("4dd7cf07b2d5f535b69b4128"), "userId" : "10010178", "userName" : "Bill Tu8", "gender" : "m8", "age" : 8, "interests" : { "game" : "game8", "ball" :
"ball8", "other" : "nothing8" } }
{ "_id" : ObjectId("4dd7cf07b2d5f535b69b4129"), "userId" : "10010179", "userName" : "Bill Tu9", "gender" : "m9", "age" : 9, "interests" : { "game" : "game9", "ball" :
"ball9", "other" : "nothing9" } }
> db.data_test.find({"age":{$gte:2},"age":{$lte:5}})//查询age>=2 and age<=5
{ "_id" : ObjectId("4dd7cf07b2d5f535b69b4121"), "userId" : "10010171", "userName" : "Bill Tu1", "gender" : "m1", "age" : 1, "interests" : { "game" : "game1", "ball" :
"ball1", "other" : "nothing1" } }
{ "_id" : ObjectId("4dd7cf07b2d5f535b69b4122"), "userId" : "10010172", "userName" : "Bill Tu2", "gender" : "m2", "age" : 2, "interests" : { "game" : "game2", "ball" :
"ball2", "other" : "nothing2" } }
{ "_id" : ObjectId("4dd7cf07b2d5f535b69b4123"), "userId" : "10010173", "userName" : "Bill Tu3", "gender" : "m3", "age" : 3, "interests" : { "game" : "game3", "ball" :
"ball3", "other" : "nothing3" } }
{ "_id" : ObjectId("4dd7cf07b2d5f535b69b4124"), "userId" : "10010174", "userName" : "Bill Tu4", "gender" : "m4", "age" : 4, "interests" : { "game" : "game4", "ball" :
"ball4", "other" : "nothing4" } }
{ "_id" : ObjectId("4dd7cf07b2d5f535b69b4125"), "userId" : "10010175", "userName" : "Bill Tu5", "gender" : "m5", "age" : 5, "interests" : { "game" : "game5", "ball" :
"ball5", "other" : "nothing5" } }
> db.data_test.find({"rank":{$all:[7,7]}})//查询rank=all(7,7)
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb98"), "userId" : "10010177", "userName" : "Bill Tu7", "gender" : "m7", "age" : 7, "rank" : [ 7, 7, 7 ], "interests" : {
"game" : "game7", "ball" : "ball7", "other" : "nothing7" } }
> db.data_test.find({"rank":{$all:[7,7,7]}})//查询rank=all(7,7,7)
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb98"), "userId" : "10010177", "userName" : "Bill Tu7", "gender" : "m7", "age" : 7, "rank" : [ 7, 7, 7 ], "interests" : {
"game" : "game7", "ball" : "ball7", "other" : "nothing7" } }
> db.data_test.find({"userName":{$exists:false}})
> db.data_test.find({"age":{$mod:[2,0]}})//查询age%2==0
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb93"), "userId" : "10010172", "userName" : "Bill Tu2", "gender" : "m2", "age" : 2, "rank" : [ 2, 2, 2 ], "interests" : {
"game" : "game2", "ball" : "ball2", "other" : "nothing2" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb95"), "userId" : "10010174", "userName" : "Bill Tu4", "gender" : "m4", "age" : 4, "rank" : [ 4, 4, 4 ], "interests" : {
"game" : "game4", "ball" : "ball4", "other" : "nothing4" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb97"), "userId" : "10010176", "userName" : "Bill Tu6", "gender" : "m6", "age" : 6, "rank" : [ 6, 6, 6 ], "interests" : {
"game" : "game6", "ball" : "ball6", "other" : "nothing6" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb99"), "userId" : "10010178", "userName" : "Bill Tu8", "gender" : "m8", "age" : 8, "rank" : [ 8, 8, 8 ], "interests" : {
"game" : "game8", "ball" : "ball8", "other" : "nothing8" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb9b"), "userId" : "100101710", "userName" : "Bill Tu10", "gender" : "m10", "age" : 10, "rank" : [ 10, 10, 10 ], "interests" :
{ "game" : "game10", "ball" : "ball10", "other" : "nothing10" } }
> db.data_test.find({"rank":{$in:[3,4]}})//查询rank in(3,4)
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb94"), "userId" : "10010173", "userName" : "Bill Tu3", "gender" : "m3", "age" : 3, "rank" : [ 3, 3, 3 ], "interests" : {
"game" : "game3", "ball" : "ball3", "other" : "nothing3" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb95"), "userId" : "10010174", "userName" : "Bill Tu4", "gender" : "m4", "age" : 4, "rank" : [ 4, 4, 4 ], "interests" : {
"game" : "game4", "ball" : "ball4", "other" : "nothing4" } }
> db.data_test.find({"age":{$nin:[2,3]}})//查询rank not in(2,3)
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb92"), "userId" : "10010171", "userName" : "Bill Tu1", "gender" : "m1", "age" : 1, "rank" : [ 1, 1, 1 ], "interests" : {
"game" : "game1", "ball" : "ball1", "other" : "nothing1" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb95"), "userId" : "10010174", "userName" : "Bill Tu4", "gender" : "m4", "age" : 4, "rank" : [ 4, 4, 4 ], "interests" : {
"game" : "game4", "ball" : "ball4", "other" : "nothing4" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb96"), "userId" : "10010175", "userName" : "Bill Tu5", "gender" : "m5", "age" : 5, "rank" : [ 5, 5, 5 ], "interests" : {
"game" : "game5", "ball" : "ball5", "other" : "nothing5" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb97"), "userId" : "10010176", "userName" : "Bill Tu6", "gender" : "m6", "age" : 6, "rank" : [ 6, 6, 6 ], "interests" : {
"game" : "game6", "ball" : "ball6", "other" : "nothing6" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb98"), "userId" : "10010177", "userName" : "Bill Tu7", "gender" : "m7", "age" : 7, "rank" : [ 7, 7, 7 ], "interests" : {
"game" : "game7", "ball" : "ball7", "other" : "nothing7" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb99"), "userId" : "10010178", "userName" : "Bill Tu8", "gender" : "m8", "age" : 8, "rank" : [ 8, 8, 8 ], "interests" : {
"game" : "game8", "ball" : "ball8", "other" : "nothing8" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb9a"), "userId" : "10010179", "userName" : "Bill Tu9", "gender" : "m9", "age" : 9, "rank" : [ 9, 9, 9 ], "interests" : {
"game" : "game9", "ball" : "ball9", "other" : "nothing9" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb9b"), "userId" : "100101710", "userName" : "Bill Tu10", "gender" : "m10", "age" : 10, "rank" : [ 10, 10, 10 ], "interests" :
{ "game" : "game10", "ball" : "ball10", "other" : "nothing10" } }
> db.data_test.find({$or:[{"age":{$gt:3}},{"rank":{$all:[1,1]}}]})//查询age>3 or rank=all(1,1)
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb92"), "userId" : "10010171", "userName" : "Bill Tu1", "gender" : "m1", "age" : 1, "rank" : [ 1, 1, 1 ], "interests" : {
"game" : "game1", "ball" : "ball1", "other" : "nothing1" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb95"), "userId" : "10010174", "userName" : "Bill Tu4", "gender" : "m4", "age" : 4, "rank" : [ 4, 4, 4 ], "interests" : {
"game" : "game4", "ball" : "ball4", "other" : "nothing4" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb96"), "userId" : "10010175", "userName" : "Bill Tu5", "gender" : "m5", "age" : 5, "rank" : [ 5, 5, 5 ], "interests" : {
"game" : "game5", "ball" : "ball5", "other" : "nothing5" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb97"), "userId" : "10010176", "userName" : "Bill Tu6", "gender" : "m6", "age" : 6, "rank" : [ 6, 6, 6 ], "interests" : {
"game" : "game6", "ball" : "ball6", "other" : "nothing6" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb98"), "userId" : "10010177", "userName" : "Bill Tu7", "gender" : "m7", "age" : 7, "rank" : [ 7, 7, 7 ], "interests" : {
"game" : "game7", "ball" : "ball7", "other" : "nothing7" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb99"), "userId" : "10010178", "userName" : "Bill Tu8", "gender" : "m8", "age" : 8, "rank" : [ 8, 8, 8 ], "interests" : {
"game" : "game8", "ball" : "ball8", "other" : "nothing8" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb9a"), "userId" : "10010179", "userName" : "Bill Tu9", "gender" : "m9", "age" : 9, "rank" : [ 9, 9, 9 ], "interests" : {
"game" : "game9", "ball" : "ball9", "other" : "nothing9" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb9b"), "userId" : "100101710", "userName" : "Bill Tu10", "gender" : "m10", "age" : 10, "rank" : [ 10, 10, 10 ], "interests" :
{ "game" : "game10", "ball" : "ball10", "other" : "nothing10" } }
> db.data_test.find({$nor:[{"age":{$gt:3}},{"rank":{$all:[1,1]}}]})//查询not (age>3 or rank=all(1,1))
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb93"), "userId" : "10010172", "userName" : "Bill Tu2", "gender" : "m2", "age" : 2, "rank" : [ 2, 2, 2 ], "interests" : {
"game" : "game2", "ball" : "ball2", "other" : "nothing2" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb94"), "userId" : "10010173", "userName" : "Bill Tu3", "gender" : "m3", "age" : 3, "rank" : [ 3, 3, 3 ], "interests" : {
"game" : "game3", "ball" : "ball3", "other" : "nothing3" } }
> db.data_test.find({"rank":{$size:3}})//查询rank数组大小为3的记录
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb92"), "userId" : "10010171", "userName" : "Bill Tu1", "gender" : "m1", "age" : 1, "rank" : [ 1, 1, 1 ], "interests" : {
"game" : "game1", "ball" : "ball1", "other" : "nothing1" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb93"), "userId" : "10010172", "userName" : "Bill Tu2", "gender" : "m2", "age" : 2, "rank" : [ 2, 2, 2 ], "interests" : {
"game" : "game2", "ball" : "ball2", "other" : "nothing2" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb94"), "userId" : "10010173", "userName" : "Bill Tu3", "gender" : "m3", "age" : 3, "rank" : [ 3, 3, 3 ], "interests" : {
"game" : "game3", "ball" : "ball3", "other" : "nothing3" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb95"), "userId" : "10010174", "userName" : "Bill Tu4", "gender" : "m4", "age" : 4, "rank" : [ 4, 4, 4 ], "interests" : {
"game" : "game4", "ball" : "ball4", "other" : "nothing4" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb96"), "userId" : "10010175", "userName" : "Bill Tu5", "gender" : "m5", "age" : 5, "rank" : [ 5, 5, 5 ], "interests" : {
"game" : "game5", "ball" : "ball5", "other" : "nothing5" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb97"), "userId" : "10010176", "userName" : "Bill Tu6", "gender" : "m6", "age" : 6, "rank" : [ 6, 6, 6 ], "interests" : {
"game" : "game6", "ball" : "ball6", "other" : "nothing6" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb98"), "userId" : "10010177", "userName" : "Bill Tu7", "gender" : "m7", "age" : 7, "rank" : [ 7, 7, 7 ], "interests" : {
"game" : "game7", "ball" : "ball7", "other" : "nothing7" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb99"), "userId" : "10010178", "userName" : "Bill Tu8", "gender" : "m8", "age" : 8, "rank" : [ 8, 8, 8 ], "interests" : {
"game" : "game8", "ball" : "ball8", "other" : "nothing8" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb9a"), "userId" : "10010179", "userName" : "Bill Tu9", "gender" : "m9", "age" : 9, "rank" : [ 9, 9, 9 ], "interests" : {
"game" : "game9", "ball" : "ball9", "other" : "nothing9" } }
{ "_id" : ObjectId("4dd7d214b2d55d5e1db1bb9b"), "userId" : "100101710", "userName" : "Bill Tu10", "gender" : "m10", "age" : 10, "rank" : [ 10, 10, 10 ], "interests" :
{ "game" : "game10", "ball" : "ball10", "other" : "nothing10" } }

相关阅读:

MongoDB Linux下的安装和启动

MongoDB下的高级查询示例

MongoDB Java API for 插入和单collection基本查询使用示例

MongoDB下的查询操作(与Java API查询操作对应)

linux


推荐阅读
  • 开发笔记:Mongodb副本集集群搭建 ... [详细]
  • 本文详细介绍了如何构建MongoDB的ReplSet复制集群,包括环境准备、配置文件设置以及初始化复制集群的具体步骤。 ... [详细]
  • 2012年7月30日,语言岛团队宣布其智能记单词软件V0.3.4.554版本正式开源。该版本不仅支持跨平台使用,还引入了多项创新功能,旨在帮助用户更高效地记忆单词。 ... [详细]
  • 在编译BSP包过程中,遇到了一个与 'gets' 函数相关的编译错误。该问题通常发生在较新的编译环境中,由于 'gets' 函数已被弃用并视为安全漏洞。本文将详细介绍如何通过修改源代码和配置文件来解决这一问题。 ... [详细]
  • 本文详细介绍了如何在Ubuntu的Enlightenment (E17) 桌面环境中管理和优化桌面图标及根菜单。通过本文,您将了解这些功能的作用及其配置方法。 ... [详细]
  • CentOS 7.6环境下Prometheus与Grafana的集成部署指南
    本文旨在提供一套详细的步骤,指导读者如何在CentOS 7.6操作系统上成功安装和配置Prometheus 2.17.1及Grafana 6.7.2-1,实现高效的数据监控与可视化。 ... [详细]
  • 本文详细介绍了 Linux 系统中用户、组和文件权限的设置方法,包括基本权限(读、写、执行)、特殊权限(SUID、SGID、Sticky Bit)以及相关配置文件的使用。 ... [详细]
  • 本文详细探讨了Java命令行参数的概念、使用方法及在实际编程中的应用,包括如何通过命令行传递参数给Java程序,以及如何在Java程序中解析这些参数。 ... [详细]
  • 本文介绍了如何利用MongoDB的$exists操作符在Java应用程序中检查特定字段是否存在于文档中,包括示例代码和解释。 ... [详细]
  • 字节跳动夏季招聘面试经验分享
    本文详细记录了字节跳动夏季招聘的面试经历,涵盖了一、二、三轮面试的技术问题及项目讨论,旨在为准备类似面试的求职者提供参考。 ... [详细]
  • 利用 Jest 和 Supertest 实现接口测试的全面指南
    本文深入探讨了如何使用 Jest 和 Supertest 进行接口测试,通过实际案例详细解析了测试环境的搭建、测试用例的编写以及异步测试的处理方法。 ... [详细]
  • 58同城的Elasticsearch应用与平台构建实践
    本文由58同城高级架构师于伯伟分享,由陈树昌编辑整理,内容源自DataFunTalk。文章探讨了Elasticsearch作为分布式搜索和分析引擎的应用,特别是在58同城的实施案例,包括集群优化、典型应用实例及自动化平台建设等方面。 ... [详细]
  • 近期参与了一个旨在提高在线平台大规模查询响应速度的项目,预计处理的数据量为2-3亿条,数据库并发量约为每秒1500次,未来可能增至3000次。通过对比Redis和MongoDB,最终选择了MongoDB,因其具备优秀的横向扩展性和GridFS支持下的Map/Reduce功能。 ... [详细]
  • ArchSummit深圳2014将于7月18日拉开帷幕,所有讲师已确认,涵盖9个热门话题,共36场精彩报告。InfoQ中文站提供了详细的讲师和报告列表。 ... [详细]
  • Linux环境下进程间通信:深入解析信号机制
    本文详细探讨了Linux系统中信号的生命周期,从信号生成到处理函数执行完毕的全过程,并介绍了信号编程中的注意事项和常见应用实例。通过分析信号在进程中的注册、注销及处理过程,帮助读者理解如何高效利用信号进行进程间通信。 ... [详细]
author-avatar
瓦斯的2010_217
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有