mongodb 查询条件

默北 mongodbmongodb 查询条件已关闭评论108,22315字数 8509阅读28分21秒阅读模式

这节来说说mongodb条件操作符,"$lt", "$lte", "$gt", "$gte", "$ne"就是全部的比较操作符,对应于"<", "<=", ">", ">=","!="。

原子操作符:"$and“, "$or“, "$nor“。文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-conditional-operators/

or查询有两种方式:一种是用$in来查询一个键的多个值,另一种是用$or来完成多个键值的任意给定值。$in相当于SQL语句的in操作。文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-conditional-operators/

$nin不属于。文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-conditional-operators/

$not与正则表达式联合使用时候极其有用,用来查询哪些与特定模式不匹配的文档。文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-conditional-operators/

$slice相当于数组函数的切片,检索一个数组文档并获取数组的一部分。限制集合中大量元素节省带宽。理论上可以通过 limit() 和 skip() 函数来实现,但是,对于数组就无能为力了。 $slice可以指定两个参数。第一个参数表示要返回的元素总数。第二个参数是可选的。如果使用的话,第一个参数定义的是偏移量,而第二个参数是限定的个数。第二个参数还可以指定一个负数。文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-conditional-operators/

$mod取摸操作。文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-conditional-operators/

$size操作符允许对结果进行筛选,匹配指定的元素数的数组。文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-conditional-operators/

$exists操作符允许返回一个特定的对象。注意:当前版本$exists是无法使用索引的,因此,使用它需要全表扫描。文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-conditional-operators/

$type操作符允许基于BSON类型来匹配结果。文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-conditional-operators/

1. 插入一些数据文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-conditional-operators/

> 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)文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-conditional-operators/

> 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)文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-conditional-operators/

> 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)文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-conditional-operators/

> 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)文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-conditional-operators/

> 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. 组合使用文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-conditional-operators/

> 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)文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-conditional-operators/

> 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文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-conditional-operators/

> 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文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-conditional-operators/

> 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文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-conditional-operators/

与$in有点相似,只不过$all是所有属性要与文档匹配。$in只匹配其一就行。文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-conditional-operators/

> 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"
                        }
                ]
        }
]
weinxin
我的微信
微信公众号
扫一扫关注运维生存时间公众号,获取最新技术文章~
默北
  • 本文由 发表于 31/05/2014 01:00:04
  • 转载请务必保留本文链接:https://www.ttlsa.com/mongodb/mongodb-conditional-operators/