mongodb 查询条件

 

这节来说说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. 插入一些数据

 
1
2
3
4
5
> 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)

 
1
2
3
4
5
6
7
8
9
> 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)

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
> 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)

 
1
2
3
4
5
6
7
8
9
> 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)

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
> 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. 组合使用

 
1
2
3
4
5
6
7
8
9
> 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)

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
> 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

 
1
2
3
4
5
6
7
8
9
> db.mediaCollection.find( {Released : {$in : [1999,2008,2009] } }, { "Cast" : 0 } ).toArray()
[
        {
                "_id" : ObjectId("53548225d85b463e729a2e57"),
                "Type" : "DVD",
                "Title" : "Matrix, The",
                "Released" : 1999
        }
]
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
> 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

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
> 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只匹配其一就行。

 
1
2
> db.mediaCollection.find( { Released : {$all : ["2010","2009"] } }, { "Cast" : 0 } ).toArray()
[ ]

11.  多个表达式

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
> 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
        }
]
 
1
2
3
4
5
6
7
8
9
> 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. 切片

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
> 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

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
> 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

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
> 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

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
> 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

下面这个实例是查询嵌入对象。

 

mongodb 查询条件

 

这节来说说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. 插入一些数据

 
1
2
3
4
5
> 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)

 
1
2
3
4
5
6
7
8
9
> 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)

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
> 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)

 
1
2
3
4
5
6
7
8
9
> 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)

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
> 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. 组合使用

 
1
2
3
4
5
6
7
8
9
> 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)

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
> 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

 
1
2
3
4
5
6
7
8
9
> db.mediaCollection.find( {Released : {$in : [1999,2008,2009] } }, { "Cast" : 0 } ).toArray()
[
        {
                "_id" : ObjectId("53548225d85b463e729a2e57"),
                "Type" : "DVD",
                "Title" : "Matrix, The",
                "Released" : 1999
        }
]
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
> 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

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
> 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只匹配其一就行。

 
1
2
> db.mediaCollection.find( { Released : {$all : ["2010","2009"] } }, { "Cast" : 0 } ).toArray()
[ ]

11.  多个表达式

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
> 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
        }
]
 
1
2
3
4
5
6
7
8
9
> 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. 切片

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
> 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

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
> 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

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
> 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

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
> 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

下面这个实例是查询嵌入对象。

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
> 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"
                        }
                ]
        }
]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
> 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"
                        }
                ]
        }
]

原文地址:http://www.ttlsa.com/mongodb/mongodb-conditional-operators/

mongodb系列:http://www.ttlsa.com/nosql/mongodb/

[转]mongodb 查询条件:关系运算符"$lt", "$lte", "$gt", "$gte", "$ne" 逻辑运算符"$and“, "$or“, "$nor“的更多相关文章

  1. 深入理解mongodb查询条件语句

    阅读目录 1. 理解:"$lt"."$lte"."$gt" 和 "$gte" 2. 理解 '$ne' 3. 理解 &qu ...

  2. mongodb 查询条件

    这节来说说mongodb条件操作符,"$lt", "$lte", "$gt", "$gte", "$ne&qu ...

  3. MongoDB查询条件常用设置

    原文地址:http://blog.csdn.net/mcpang/article/details/8731065 Java操作mongodb进行查询,常用筛选条件的设置如下: 条件列表: BasicD ...

  4. java 操作mongodb查询条件的常用设置

    java操作mongodb进行查询,常用筛选条件的设置如下: 条件列表:BasicDBList condList = new BasicDBList(); 临时条件对象:BasicDBObject c ...

  5. 65.ORM查询条件:gte,gt,lte和lt的使用

    1. gte: 代表的是大于等于,英文全称为:great than equal.举例:找到文章id大于等于3等文章,示例代码如下: 定义模型的示例代码如下: from django.db import ...

  6. mongodb复杂条件查询 (or与and)

    分类专栏: mongodb   版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/tjbsl/ ...

  7. MongoDB查询内嵌数组(限定返回符合条件的数组中的数据)(1)

    https://blog.csdn.net/bicheng4769/article/details/79579830 项目背景 最近在项目中使用mongdb来保存压测结果中的监控数据,那么在获取监控数 ...

  8. 【Mongodb教程 第十九课 】PHP与MONGODB的条件查询

    与普通的关系型数据库类似,在对数据的删.改.查的时候,会用到查询条件,如mysql中的 where… 而MongoDB中,经过php来做的所有的操作指令都是用array来包裹的: MongoColle ...

  9. mongodb多条件分页查询的三种方法(转)

    一.使用limit和skip进行分页查询 public List<User> pageList(int pageNum ,int pageSize){ List<User> u ...

随机推荐

  1. delphi下如何获得不带扩展名的文件名?

    Edit1.Text:=ChangeFileExt(ExtractFileName(Application.ExeName),'') ; //获取到应用程序名后,将后缀名清空就可以啦.

  2. 管子&小白

    管夷吾已入朝,稽首谢罪,桓公亲手扶起,赐之以坐.夷吾曰:“臣乃俘戮之余,得蒙宥死,实为万幸,敢辱过礼!”桓公曰:“寡人有问于子,子必坐,然后敢请."夷吾再拜就坐. 桓公曰:“齐,千乘之国,先 ...

  3. php--数据库三范式

    关系数据库的几种设计范式介绍1.第一范式(1NF) 在任何一个关系数据库中,第一范式(1NF)是对关系模式的基本要求,不满足第一范式(1NF)的数据库就不是关系数据库. 所谓第一范式(1NF)是指数据 ...

  4. android 资源ID

    在编译的时候,AAPT会扫描你所定义的所有资源(在不同文件中定义的以及单独的资源文件),然后给它们指定不同的资源ID.资源ID 是一个32bit的数字,格式是PPTTNNNN , PP代表资源所属的包 ...

  5. threading多线程

    什么是线程? 线程是操作系统能够进行运算调度的最小单位.它被包含在进程之中,是进程中的实际运作单位.一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任务.一 ...

  6. Mongoose中关联查询populate的使用

    MongoDB中没有join的特性,因此无法使用join进行表的连接和关联查询,在Mongoose中封装了populate方法,在定义一个 Schema 的时候可以指定了其中的字段(属性)是另一个Sc ...

  7. sqlserver 中含有某字符串

    查找 sqlserver 中字符串的ascii码SET TEXTSIZE 0-- Create variables for the character string and for the curre ...

  8. appium testcase1(Java)

    官网源码地址 https://github.com/appium/sample-code/blob/47fc0305396b8322b727820ca55a07607395040c/sample-co ...

  9. linux i2c 设备节点读写

    最近需要操作24C02,封装了一下函数方便以后操作. 参考链接: https://my.oschina.net/handawei/blog/68526 http://blog.csdn.net/one ...

  10. mysql日期加减

    mysql日期加减一.MySQL 为日期增加一个时间间隔:date_add().1.  示例:set @dt = now();select date_add(@dt, interval 1 day); ...