function find(query, fields, limit, skip)
1,多个where条件
// i.e., select * from things where x=3 and y="foo"
db.things.find( { x : 3, y : "foo" } );
db.things.find({j: {$ne: 3}, k: {$gt: 10} });
2,查询部分字段
// select z from things where x="john"(查询字段z)
db.things.find( { x : "john" }, { z : 1 } );
// get all posts about 'tennis' but without the comments field(查询除了comments以外的所有字段)
db.posts.find( { tags : 'tennis' }, { comments : 0 } );
默认查询出来的结果包含_id字段,如果不想要,你可以排除他
db.things.find( { x : "john" }, { z : 1 , _id : 0} );
// 查询子文档的部分字段
t.find({})
"_id" : ObjectId("4c23f0486dad1c3a68457d20"), "x" : { "y" : 1, "z" : [ 1, 2, 3 ] } }
t.find({}, {'x.y':1})
"_id" : ObjectId("4c23f0486dad1c3a68457d20"), "x" : { "y" : 1 } }
// 数组截取
db.posts.find({}, {comments:{$slice: 5}}) // first 5 comments
db.posts.find({}, {comments:{$slice: -5}}) // last 5 comments
db.posts.find({}, {comments:{$slice: [20, 10]}}) // skip 20, limit 10
db.posts.find({}, {comments:{$slice: [-20, 10]}}) // 20 from end, limit 10
3,比较操作符($gt,$lt,$gte,$lte,$ne)
db.collection.find({ "field" : { $gt: value } } ); // greater than : field > value
db.collection.find({ "field" : { $lt: value } } ); // less than : field < value
db.collection.find({ "field" : { $gte: value } } ); // greater than or equal to : field >= value
db.collection.find({ "field" : { $lte: value } } ); // less than or equal to : field <= value
db.things.find( { x : { $ne : 3 } } ); // not equals : field != value
db.collection.find({ "field" : { $gt: value1, $lt: value2 } } ); // value1 < field < value
4,必须包含$all
数组可以比$all操作符对应的数据含有更加多的元素,$all表示满足条件元素的最小集合
{ a: [ 1, 2, 3 ] }
db.things.find( { a: { $all: [ 2, 3 ] } } ); // match
db.things.find( { a: { $all: [ 2, 3, 4 ] } } ); // not match
5,是否存在$exists
db.things.find( { a : { $exists : true } } ); // return object if a is present
db.things.find( { a : { $exists : false } } ); // return if a is missing
6,取模运算符$mod
db.things.find( "this.a % 10 == 1");
db.things.find( { a : { $mod : [ 10 , 1 ] } } ) ; //better
7,IN,NOTIN运算符$in,$nin
db.collection.find( { "field" : { $in : array } } );
db.things.find({j:{$in: [2,4,6]}});
db.things.find({j:{$nin: [2,4,6]}});
8,OR,NOT OR, NOT运算符$or,$nor,$not
db.foo.find( { $or : [ { a : 1 } , { b : 2 } ] } )
db.foo.find( { name : "bob" , $or : [ { a : 1 } , { b : 2 } ] } )
db.customers.find( { name : { $not : /acme.*corp/i } } );
db.things.find( { a : { $not : { $mod : [ 10 , 1 ] } } } );
9,数组元素个数运算符$size
$size不支持范围操作,只能是size = 操作
db.things.find( { a : { $size: 1 } } );
10,BSON元素类型判断运算符$type
db.things.find( { a : { $type : 2 } } ); // matches if a is a string
db.things.find( { a : { $type : 16 } } ); // matches if a is an int
Type Name Type Number
Double 1
String 2
Object 3
Array 4
Binary data 5
Object id 7
Boolean 8
Date 9
Null 10
Regular expression 11
JavaScript code 13
Symbol 14
JavaScript code with scope 15
32-bit integer 16
Timestamp 17
64-bit integer 18
Min key 255
Max key 127
11,正则表达式
db.customers.find( { name : /acme.*corp/i } );
12,值是否在数组,值是否在嵌套对象
db.things.find( { colors : "red" } ); // To look for the value "red" in an array field colors:
db.postings.find( { "author.name" : "joe" } ); // For example, to look author.name=="joe" in a postings collection with embedded author objects:
13,遍历数组$elemMatch
Use $elemMatch to check if an element in an array matches the specified match expression.
t.find( { x : { $elemMatch : { a : 1, b : { $gt : 1 } } } } )
{ "_id" : ObjectId("4b5783300334000000000aa9"),
"x" : [ { "a" : 1, "b" : 3 }, 7, { "b" : 99 }, { "a" : 11 } ]
}
t.find( { "x.a" : 1, "x.b" : { $gt : 1 } } )
14,JavaScript表达式和$where
下面的4个表达式等价!!JavaScript表达式的执行效率比使用上述操作符的执行时间差些,不过使用起来更加灵活些
db.myCollection.find( { a : { $gt: 3 } } );
db.myCollection.find( { $where: "this.a > 3" } );
db.myCollection.find("this.a > 3");
f = function() { return this.a > 3; } db.myCollection.find(f);
15,count函数
对查询结果计数,在MongoDB Server端计数比MongoDB Client端更加快速和有效
nstudents = db.students.find({'address.state' : 'CA'}).count();
nstudents = db.students.find({'address.state' : 'CA'}).toArray().length; // VERY BAD: slow and uses excess memory
count方法默认忽略count前面使用的skip,limit函数,可以使用count(true)来使得skip,limit起作用
n = db.students.find().skip(20).limit(10).count(true);
16,limit函数
db.students.find().limit(10).forEach( function(student) { print(student.name + "<p>"); } );
17,skip函数
function printStudents(pageNumber, nPerPage) {
print("Page: " + pageNumber);
db.students.find().skip((pageNumber-1)*nPerPage).limit(nPerPage).forEach( function(student) { print(student.name + "<p>"); } );
}
18,sort函数
db.myCollection.find().sort( { ts : -1 } ); // sort by ts, descending order
19,min,max函数(查询条件)
db.f.find().min({name:"barry"}}.max({name:"larry"}).hint({name:1}); // 包含小的,不包含大的
db.f.find().min({name:"barry"}}.max({name:"larry"});
db.f.find().min({last_name:"smith",first_name:"john"}};
db.f.find({$min: {name:"barry"}, $max: {name:"larry"}, $query:{}});
分享到:
相关推荐
标题中的“07-常用操作系统下的开发环境搭建-简介”表明了这个压缩包内容主要涉及的是如何在不同的操作系统上设置Python开发环境的基础介绍。描述提到是“python在常用操作系统下的开发环境搭建”,并且强调这是一个...
在IT领域,尤其是在服务器管理和网站运营中,熟练掌握Linux下的常用操作命令是至关重要的技能。以下将基于给定文件中的信息,深入解析与Linux操作系统下Apache服务器和数据库管理相关的几个核心知识点。 ### 1. ...
内容概要:本文档是面向2024年Java开发者的常用操作速查指南,涵盖了面向对象编程、集合框架与泛型、基础语法速查、异常处理机制、文件与IO操作等方面的内容。每部分都提供了详细的操作示例和最佳实践,帮助开发者...
实验的目标是让你理解HBase在Hadoop架构中的地位,以及掌握通过Shell命令和Java API进行基本操作的方法。 首先,让我们来看看实验的平台配置。实验要求的操作系统是Linux,这通常是大数据处理的首选平台,因为它...
在计算机操作系统中,尤其是Windows XP系统,掌握一些常用的命令行工具和快捷键是提升操作效率的关键。"常用命令对照簿"通常包含了一系列在命令提示符(CMD)中使用的命令,以及一些基本的快捷键组合,这些都能帮助...
细数会计必备常用财务软件【会计实务操作教程】 细数会计必备常用财务软件【会计实务操作教程】是指在会计实务操作中,需要使用的常用财务软件。这些软件对于会计人员来说非常重要,因为它们可以帮助会计人员更好地...
文本框控件用于输入、编辑和显示正文内容,常用属性包括 Text、MaxLength、MultiLine 和 ScrollBars 等。文本框控件的事件包括 Change、KeyPress 和 KeyUp 等。 4. 命令按钮控件 命令按钮控件用于触发事件,例如...
电脑常用操作技巧大全 本文总结了电脑使用中的一些高效操作技巧,旨在提高用户的操作效率和体验。这些技巧涵盖了多个方面,包括键盘操作、窗口管理、文件管理、系统设置等。 一、键盘操作技巧 1. 锁定电脑屏幕:...
【大数据技术原理及应用实验3:熟悉常用的HBase操作】 HBase是一种分布式、列式存储的NoSQL数据库,它是构建在Hadoop文件系统(HDFS)之上的,用于处理大规模数据集的应用。在Hadoop生态体系中,HBase提供实时读写...
总之,"vb编程常用必备图标"这个资源对于VB初学者和经验丰富的开发者都是宝贵的参考资料,它涵盖了各种常见操作的图标,帮助你在编程实践中快速找到合适的图形元素,为你的项目增添一份专业感。通过深入理解和恰当...
6. **文件操作类**:文件操作类如`System.IO`命名空间下的`File`、`Directory`、`FileStream`等,提供了读写文件、创建目录、移动文件等操作。这些类使得文件系统的操作变得简单易用。 7. **常用JS操作类**:在...
本文档将为您详细讲解 Linux 常用的必备命令,包括目录命令、文件操作命令、文件编辑命令、文件查看命令、文件搜索命令等。 目录命令 目录命令是 Linux 中最基本的命令,用于管理文件和目录。常用的目录命令包括:...
- **Fastboot**: 这是Android系统提供的一个命令行工具,用于在设备启动模式下执行低级操作,如刷入新的系统映像。 - **Odin**: 对于三星设备,Odin是一个官方的刷机工具,用户可以使用它来安装固件更新或恢复备份...
内容概要:本文档详细介绍了一系列常用的Linux命令及其使用方法,旨在帮助用户熟悉和掌握Linux环境下的基本文件和目录操作、文件内容查看、搜索、权限管理和文本处理等方面的基础技能。每个命令不仅附有简明扼要的...
【常用Linux基本操作】 在Linux操作系统中,掌握一些基本的命令和操作对于日常使用和管理至关重要。虽然Linux命令行提供了无数的选项和功能,但在实际应用中,我们往往只需要掌握最常用的那部分即可。本文将重点...
本篇文章将深入探讨Linux的常用操作命令,包括系统结构、命令行的含义、命令组成、文件颜色、通配符、转义字符以及基础操作和目录操作。 1. **Linux系统的文件结构** - `/bin`:存放系统基本命令 - `/boot`:存储...
linux第4章:常用操作命令,linux从入门到精通,常见命令,大神必备
"中兴常用软件root必备"这个标题表明,这是一个针对中兴手机用户,准备进行Root操作时需要用到的一系列软件集合。 首先,我们来详细了解一下"JOIN-ME"。这可能是指JOIN-ME工具,它通常用于协助Root过程,帮助设备...
计算机文件管理是计算机专业人员日常工作中最基本的操作,包括文件的创建、编辑、保存、删除等。英语词汇包括word、file、save、document、directory、folder等。 2. 编程语言:program、code、command、line、if、...