mongodb条件操作符,"$lt", "$lte", "$gt", "$gte", "$ne"就是全部的比较操作符,对应于"<", "<=", ">", ">=","!="。
原子操作符:"$and“, "$or“, "$nor“。
or查询有两种方式:一种是用$in来查询一个键的多个值,另一种是用$or来完成多个键值的任意给定值。$in相当于SQL语句的in操作。
$nin不属于。
$not与正则表达式联合使用时候极其有用,用来查询哪些与特定模式不匹配的文档。
$slice相当于数组函数的切片,检索一个数组文档并获取数组的一部分。限制集合中大量元素节省带宽。理论上可以通过 limit() 和 skip() 函数来实现,但是,对于数组就无能为力了。 $slice可以指定两个参数。第一个参数表示要返回的元素总数。第二个参数是可选的。如果使用的话,第一个参数定义的是偏移量,而第二个参数是限定的个 数。第二个参数还可以指定一个负数。
$mod取摸操作。
$size操作符允许对结果进行筛选,匹配指定的元素数的数组。
$exists操作符允许返回一个特定的对象。注意:当前版本$exists是无法使用索引的,因此,使用它需要全表扫描。
$type操作符允许基于BSON类型来匹配结果。
1. 插入一些数据
> use ttlsa_com switched to db ttlsa_com > db.mediaCollection.insert({ "Type" : "DVD", "Title" : "Matrix, The", "Released" : 1999, "Cast" : ["Keanu Reeves","Carry-Anne Moss","Laurence Fishburne","Hugo Weaving","Gloria Foster","Joe Pantoliano"] }) > db.mediaCollection.insert({ "Type" : "DVD", Title : "Blade Runner", Released : 1982 }) > db.mediaCollection.insert({ "Type" : "DVD", Title : "Toy Story 3", Released : 2010 })
2. $gt (greater than)
> db.mediaCollection.find({ Released : {$gt : 2000} }, { "Cast" : 0 }).toArray() [ { "_id" : ObjectId("53548254d85b463e729a2e59"), "Type" : "DVD", "Title" : "Toy Story 3", "Released" : 2010 } ]
3. $gte(greater than or equal to)
> db.mediaCollection.find( { Released : {$gte : 1999 } }, { "Cast" : 0 } ).toArray() [ { "_id" : ObjectId("53548225d85b463e729a2e57"), "Type" : "DVD", "Title" : "Matrix, The", "Released" : 1999 }, { "_id" : ObjectId("53548254d85b463e729a2e59"), "Type" : "DVD", "Title" : "Toy Story 3", "Released" : 2010 } ]
4. $lt (less than)
> db.mediaCollection.find( { Released : {$lt : 1999 } }, { "Cast" : 0 } ).toArray() [ { "_id" : ObjectId("5354823fd85b463e729a2e58"), "Type" : "DVD", "Title" : "Blade Runner", "Released" : 1982 } ]
5. $lte (less than or equal to)
> db.mediaCollection.find( {Released : {$lte: 1999}}, { "Cast" : 0 } ).toArray() [ { "_id" : ObjectId("53548225d85b463e729a2e57"), "Type" : "DVD", "Title" : "Matrix, The", "Released" : 1999 }, { "_id" : ObjectId("5354823fd85b463e729a2e58"), "Type" : "DVD", "Title" : "Blade Runner", "Released" : 1982 } ]
6. 组合使用
> db.mediaCollection.find( {Released : {$gte: 1990, $lt : 2010}}, { "Cast" : 0 } ).toArray() [ { "_id" : ObjectId("53548225d85b463e729a2e57"), "Type" : "DVD", "Title" : "Matrix, The", "Released" : 1999 } ]
7. $ne (not equals)
> db.mediaCollection.find( { Type : "DVD"} ).toArray() [ { "_id" : ObjectId("53548225d85b463e729a2e57"), "Type" : "DVD", "Title" : "Matrix, The", "Released" : 1999, "Cast" : [ "Keanu Reeves", "Carry-Anne Moss", "Laurence Fishburne", "Hugo Weaving", "Gloria Foster", "Joe Pantoliano" ] }, { "_id" : ObjectId("5354823fd85b463e729a2e58"), "Type" : "DVD", "Title" : "Blade Runner", "Released" : 1982 }, { "_id" : ObjectId("53548254d85b463e729a2e59"), "Type" : "DVD", "Title" : "Toy Story 3", "Released" : 2010 } ] > db.mediaCollection.find( { Type : "DVD", Released : { $ne: 1999}} ).toArray() [ { "_id" : ObjectId("5354823fd85b463e729a2e58"), "Type" : "DVD", "Title" : "Blade Runner", "Released" : 1982 }, { "_id" : ObjectId("53548254d85b463e729a2e59"), "Type" : "DVD", "Title" : "Toy Story 3", "Released" : 2010 } ]
8. $in/$or
> db.mediaCollection.find( {Released : {$in : [1999,2008,2009] } }, { "Cast" : 0 } ).toArray() [ { "_id" : ObjectId("53548225d85b463e729a2e57"), "Type" : "DVD", "Title" : "Matrix, The", "Released" : 1999 } ]
> db.mediaCollection.find( {$or : [ {Released:1999}, {Released:2008}, {Released:2009} ] } ).toArray() [ { "_id" : ObjectId("53548225d85b463e729a2e57"), "Type" : "DVD", "Title" : "Matrix, The", "Released" : 1999, "Cast" : [ "Keanu Reeves", "Carry-Anne Moss", "Laurence Fishburne", "Hugo Weaving", "Gloria Foster", "Joe Pantoliano" ] } ]
9. $nin
> db.mediaCollection.find( {Type : "DVD" }, { "Cast" : 0 }).toArray() [ { "_id" : ObjectId("53548225d85b463e729a2e57"), "Type" : "DVD", "Title" : "Matrix, The", "Released" : 1999 }, { "_id" : ObjectId("5354823fd85b463e729a2e58"), "Type" : "DVD", "Title" : "Blade Runner", "Released" : 1982 }, { "_id" : ObjectId("53548254d85b463e729a2e59"), "Type" : "DVD", "Title" : "Toy Story 3", "Released" : 2010 } ] > db.mediaCollection.find( {Released : {$nin : [1999,2008,2009] },Type : "DVD" }, { "Cast" : 0 }).toArray() [ { "_id" : ObjectId("5354823fd85b463e729a2e58"), "Type" : "DVD", "Title" : "Blade Runner", "Released" : 1982 }, { "_id" : ObjectId("53548254d85b463e729a2e59"), "Type" : "DVD", "Title" : "Toy Story 3", "Released" : 2010 } ]
10. $all
与$in有点相似,只不过$all是所有属性要与文档匹配。$in只匹配其一就行。
> db.mediaCollection.find( { Released : {$all : ["2010","2009"] } }, { "Cast" : 0 } ).toArray() [ ]
11. 多个表达式
> db.mediaCollection.find({ $or : [ { "Title" : "Toy Story 3" }, { "ISBN" : "987-1-4302-3051-9" } ] } ).toArray() [ { "_id" : ObjectId("5353462f93efef02c962da71"), "Type" : "Book", "Title" : "Definitive Guide to MongoDB, the", "ISBN" : "987-1-4302-3051-9", "Publisher" : "Apress", "Author" : [ "Membrey, Peter", "Plugge, Eelco", "Hawkins, Tim" ] }, { "_id" : ObjectId("53548254d85b463e729a2e59"), "Type" : "DVD", "Title" : "Toy Story 3", "Released" : 2010 } ]
> db.mediaCollection.find({ "Type" : "DVD", $or : [ { "Title" : "Toy Story 3" }, { "ISBN" : "987-1-4302-3051-9" } ] }).toArray() [ { "_id" : ObjectId("53548254d85b463e729a2e59"), "Type" : "DVD", "Title" : "Toy Story 3", "Released" : 2010 } ]
11. 切片
> db.mediaCollection.find({"Title" : "Matrix, The"}).toArray() [ { "_id" : ObjectId("53548225d85b463e729a2e57"), "Type" : "DVD", "Title" : "Matrix, The", "Released" : 1999, "Cast" : [ "Keanu Reeves", "Carry-Anne Moss", "Laurence Fishburne", "Hugo Weaving", "Gloria Foster", "Joe Pantoliano" ] } ] > db.mediaCollection.find({"Title" : "Matrix, The"}, {"Cast" : {$slice: 3}}).toArray() [ { "_id" : ObjectId("53548225d85b463e729a2e57"), "Type" : "DVD", "Title" : "Matrix, The", "Released" : 1999, "Cast" : [ "Keanu Reeves", "Carry-Anne Moss", "Laurence Fishburne" ] } ] > db.mediaCollection.find({"Title" : "Matrix, The"}, {"Cast" : {$slice: -3}}).toArray() [ { "_id" : ObjectId("53548225d85b463e729a2e57"), "Type" : "DVD", "Title" : "Matrix, The", "Released" : 1999, "Cast" : [ "Hugo Weaving", "Gloria Foster", "Joe Pantoliano" ] } ] > db.mediaCollection.find({"Title" : "Matrix, The"}, {"Cast" : {$slice: [2,3] }}).toArray() [ { "_id" : ObjectId("53548225d85b463e729a2e57"), "Type" : "DVD", "Title" : "Matrix, The", "Released" : 1999, "Cast" : [ "Laurence Fishburne", "Hugo Weaving", "Gloria Foster" ] } ] > db.mediaCollection.find({"Title" : "Matrix, The"}, {"Cast" : {$slice: [-5,4] }}).toArray() [ { "_id" : ObjectId("53548225d85b463e729a2e57"), "Type" : "DVD", "Title" : "Matrix, The", "Released" : 1999, "Cast" : [ "Carry-Anne Moss", "Laurence Fishburne", "Hugo Weaving", "Gloria Foster" ] } ]
12. $mod
> db.mediaCollection.find( { Type : "DVD", Released : { $mod: [2,0] } } ).toArray() [ { "_id" : ObjectId("5354823fd85b463e729a2e58"), "Type" : "DVD", "Title" : "Blade Runner", "Released" : 1982 }, { "_id" : ObjectId("53548254d85b463e729a2e59"), "Type" : "DVD", "Title" : "Toy Story 3", "Released" : 2010 } ] > db.mediaCollection.find( { Type : "DVD", Released : { $mod: [2,1] } } ).toArray() [ { "_id" : ObjectId("53548225d85b463e729a2e57"), "Type" : "DVD", "Title" : "Matrix, The", "Released" : 1999, "Cast" : [ "Keanu Reeves", "Carry-Anne Moss", "Laurence Fishburne", "Hugo Weaving", "Gloria Foster", "Joe Pantoliano" ] } ]
13. $size
> db.mediaCollection.find( { Tracklist : {$size : 2} } ).toArray() [ { "_id" : ObjectId("5353463193efef02c962da73"), "Type" : "CD", "Artist" : "Nirvana", "Title" : "Nevermind", "Tracklist" : [ { "Track" : "1", "Title" : "Smells like teen spirit", "Length" : "5:02" }, { "Track" : "2", "Title" : "In Bloom", "Length" : "4:15" } ] } ] > db.mediaCollection.find( { Cast : {$size : 1} } ).toArray() [ ] > db.mediaCollection.find( { Cast : {$size : 6} } ).toArray() [ { "_id" : ObjectId("53548225d85b463e729a2e57"), "Type" : "DVD", "Title" : "Matrix, The", "Released" : 1999, "Cast" : [ "Keanu Reeves", "Carry-Anne Moss", "Laurence Fishburne", "Hugo Weaving", "Gloria Foster", "Joe Pantoliano" ] } ]
14. $exists
> db.mediaCollection.find( { Author : {$exists : true } } ).toArray() [ { "_id" : ObjectId("5353462f93efef02c962da71"), "Type" : "Book", "Title" : "Definitive Guide to MongoDB, the", "ISBN" : "987-1-4302-3051-9", "Publisher" : "Apress", "Author" : [ "Membrey, Peter", "Plugge, Eelco", "Hawkins, Tim" ] } ] > db.mediaCollection.find( { Author : {$exists : false } } ).toArray() [ { "_id" : ObjectId("5353462f93efef02c962da72"), "Type" : "CD", "Artist" : "Nirvana", "Title" : "Nevermind" }, { "_id" : ObjectId("5353463193efef02c962da73"), "Type" : "CD", "Artist" : "Nirvana", "Title" : "Nevermind", "Tracklist" : [ { "Track" : "1", "Title" : "Smells like teen spirit", "Length" : "5:02" }, { "Track" : "2", "Title" : "In Bloom", "Length" : "4:15" } ] }, { "_id" : ObjectId("53548225d85b463e729a2e57"), "Type" : "DVD", "Title" : "Matrix, The", "Released" : 1999, "Cast" : [ "Keanu Reeves", "Carry-Anne Moss", "Laurence Fishburne", "Hugo Weaving", "Gloria Foster", "Joe Pantoliano" ] }, { "_id" : ObjectId("5354823fd85b463e729a2e58"), "Type" : "DVD", "Title" : "Blade Runner", "Released" : 1982 }, { "_id" : ObjectId("53548254d85b463e729a2e59"), "Type" : "DVD", "Title" : "Toy Story 3", "Released" : 2010 } ]
15. $type
根据BSON类型来检索集合中匹配的结果。
MongoDB中可以使用的类型如下表所示:
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 |
下面这个实例是查询嵌入对象。
> db.mediaCollection.find ( { Tracklist: { $type : 3 } } ).toArray() [ { "_id" : ObjectId("5353463193efef02c962da73"), "Type" : "CD", "Artist" : "Nirvana", "Title" : "Nevermind", "Tracklist" : [ { "Track" : "1", "Title" : "Smells like teen spirit", "Length" : "5:02" }, { "Track" : "2", "Title" : "In Bloom", "Length" : "4:15" } ] } ]
相关推荐
产品特点别名查询参数黑名单查询参数白名单查询参数基本运算符$eq $gt $gte $lt $lte $ne $in $nin $exists $regex 解析字符串整数并浮点为数字将字符串布尔值解析为ture / false布尔值操作请求参数查询对象等于?...
* 条件查询:使用 `$gt`、`$lt`、`$gte`、`$lte` 等操作符。 * 高级查询条件操作符:<, , >, >= 等。 * `$all` 匹配所有。 * `$exists` 判断字段是否存在。 * `$mod` 取模运算。 * `$ne` 不等于。 * `$in` 包含在.....
mongodump.ext -d test -o ../data/backup/test //备份数据库test中所有的数据集合 mongorestore.exe --help mongorestore.exe -d "test" D:\Cloud\NoSql\mongodb2\data\backup\test //恢复数据库test mongorestore....
MongoDB 高级查询用法大全 MongoDB 作为一个 NoSQL 数据库,提供了多种高级查询方式,以下是 MongoDB 高级查询用法大全: 一、比较运算符 在 MongoDB 中,比较运算符用于比较字段的值,常用的比较运算符有: * $...
类似的还有`$lt`(小于)、`$gte`(大于等于)、`$lte`(小于等于)、`$ne`(不等于)等比较运算符。 - `{a:{$in:[10,"hello"]}}`:查询所有文档中字段`a`为10或"hello"的记录。 - `{a:{$all:[10,"hello"]}}`:...
特性:Comparisons Operators ($gt, $gte, $lt, $lte, $ne, $nin, $in)Logical Operators ($and, $or, $nor, $not)Evaluation Operators ($regex, $mod, $where)Array Operators ($all, $elemMatch, $size)Element ...
在MongoDB中,查询是获取数据的核心操作,而高级查询则是实现复杂数据筛选的关键。以下将详细介绍文档中提到的一些高级查询方法: 1. **比较操作符**: - `$gt`(大于): 查询`field`值大于`value`的文档。 - `$...
- **正则表达式性能**: 在使用正则表达式时,应尽量避免使用 `/^a./` 和 `/^a.$/` 这样的形式,因为它们会扫描整个字符串,从而降低查询效率。而 `/^a/` 形式仅在匹配前缀后停止扫描,因此更高效。 - **索引使用**: ...
MongoDB 查询语法详解 MongoDB 是一个基于NoSQL的数据库,具有高效、灵活、易扩展等特点。在 MongoDB 中,查询语法是非常重要的一部分,本文将对 MongoDB 的查询语法进行详细的介绍。 基本查询语法 在 MongoDB 中...
- 条件运算符:如$ne(不等于)、$in(在列表中)、$nin(不在列表中)、$mod(取模)、$all(所有)、$size(大小)、$exists(存在)、$type(类型)等,以及比较运算符如$lt(小于)、$lte(小于等于)、$gt(大于)、$gte(大于等于)。...
### MongoDB查询详解 #### 一、引言 在NoSQL数据库的世界里,MongoDB因其灵活的数据模型、高性能和可扩展性而备受青睐。查询作为数据库操作中最基础也是最重要的功能之一,在MongoDB中同样有着丰富的语法支持。...
Mongodb 查询到 sql 查询转换器。 示例:在:db.user.find({name: 'julio'})... 翻译器支持以下 mongodb 操作符: $or $and (记住 $and 和对象上的逗号分隔值是相同的) $lt $lte $gt $gte $ne $in 输入文件:input_
MongoDB 提供了丰富的查询操作符,如 `$eq`(等于)、`$ne`(不等于)、`$lt`(小于)、`$lte`(小于或等于)、`$gt`(大于)、`$gte`(大于或等于)、`$in`(在数组内)、`$nin`(不在数组内)等,这些操作符允许你...
关系运算是MongoDB支持的关系查询操作,包括大于($gt)、小于($lt)、大于等于($gte)、小于等于($lte)、不等于($ne)、等于(key:value、$eq)。例如: * 查询姓名是张三的学生信息 * 查询性别是男的学生...
在本文中,我们将深入探讨 MongoDB 的普通查询操作,包括基本查询语法、AND 和 OR 条件查询。 1. **查询语法** MongoDB 提供了 `find()` 方法来查询集合(COLLECTION_NAME)中的文档。基础用法如下: ```...
还支持 `$lt`(小于)、`$gte`(大于等于)、`$lte`(小于等于)、`$ne`(不等于)。 4. **值列表匹配**:`{a: {$in: [10, "hello"]}}` - 匹配 `a` 字段值为 10 或 "hello" 的文档。 5. **数组元素全匹配**:`{a...
在MongoDB中,查询操作是获取数据的关键部分,本教程将深入讲解如何在MongoDB中进行各种查询操作。 1. **基本查询** - `findOne()` 方法用于查找集合中的单个文档。例如: ```javascript > db.test.findOne() `...
1. **比较运算符**:`$gt`, `$gte`, `$lt`, `$lte`, `$ne`和`=`分别对应大于、大于等于、小于、小于等于、不等于和等于,例如`db.users.find({age: {$gt: 30}})`。 2. **逻辑运算符**:`$or`, `$in`, `$nin`用于...
MongoDB提供了`$gt`(大于)、`$lt`(小于)、`$gte`(大于或等于)和`$lte`(小于或等于)操作符来筛选特定范围内的数据。例如,要查找`field`字段值大于`value`的文档,可以使用`db.collection.find({ "field" : {...