上一篇 MongoDB查询文档(1) 中介绍了MongoDB查询文档中使用比较筛选和逻辑筛选,这里我们继续介绍MongoDB的文档查询,这里我们介绍元素筛选、数组筛选。
语法:{ field: { $exists:
例子:
查询集合test所有的文档:
{ "_id" : ObjectId("642be59ec2efd6c6eacc71fd"), "id" : "1002", "name" : "李四", "age" : 22 }
{ "_id" : ObjectId("642be59ec2efd6c6eacc71fe"), "id" : "1003", "name" : "王五", "age" : 25 }
{ "_id" : ObjectId("642d273d5f3cbd93e8be96c4"), "id" : "1001", "name" : "张三", "age" : 20 }
{ "_id" : ObjectId("642d27485f3cbd93e8be96c5"), "id" : "1004", "name" : "赵六" }
下面我们查询年龄存在的成员:
db.test.find({"age": { $exists: true }});
查询的结果如下:
{ "_id" : ObjectId("642be59ec2efd6c6eacc71fd"), "id" : "1002", "name" : "李四", "age" : 22 }
{ "_id" : ObjectId("642be59ec2efd6c6eacc71fe"), "id" : "1003", "name" : "王五", "age" : 25 }
{ "_id" : ObjectId("642d273d5f3cbd93e8be96c4"), "id" : "1001", "name" : "张三", "age" : 20 }
下面我们查询年龄不存在的成员:
db.test.find({"age": { $exists: false }});
查询的结果如下:
{ "_id" : ObjectId("642d27485f3cbd93e8be96c5"), "id" : "1004", "name" : "赵六" }
语法:{ field: { $type:
或 { field: { $type: [
其中BSON type可以使用别名也可以使用编号,如下:
类型 |
编号 |
别名 |
---|---|---|
Double |
1 |
"double" |
String |
2 |
"string" |
Object |
3 |
"object" |
Array |
4 |
"array" |
Binary data |
5 |
"binData" |
Undefined |
6 |
"undefined" |
ObjectId |
7 |
"objectId" |
Boolean |
8 |
"bool" |
Date |
9 |
"date" |
Null |
10 |
"null" |
Regular Expression |
11 |
"regex" |
DBPointer |
12 |
"dbPointer" |
JavaScript |
13 |
"javascript" |
Symbol |
14 |
"symbol" |
JavaScript code with scope |
15 |
"javascriptWithScope" |
32-bit integer |
16 |
"int" |
Timestamp |
17 |
"timestamp" |
64-bit integer |
18 |
"long" |
Decimal128 |
19 |
"decimal" |
Min key |
-1 |
"minKey" |
Max key |
127 |
"maxKey" |
例子:
下面我们查询名称为字符串的成员:
db.test.find({"name": { $type: "string" }});
此查询等效于:
db.test.find({"name": { $type: 2 }});
查询的结果如下:
{ "_id" : ObjectId("642be59ec2efd6c6eacc71fd"), "id" : "1002", "name" : "李四", "age" : 22 }
{ "_id" : ObjectId("642be59ec2efd6c6eacc71fe"), "id" : "1003", "name" : "王五", "age" : 25 }
{ "_id" : ObjectId("642d273d5f3cbd93e8be96c4"), "id" : "1001", "name" : "张三", "age" : 20 }
{ "_id" : ObjectId("642d27485f3cbd93e8be96c5"), "id" : "1004", "name" : "赵六" }
语法:{
例子:
查询集合test所有的文档:
{ "_id" : "1001", "name" : "张三", "fruits" : [ "apple", "orange" ] }
{ "_id" : "1002", "name" : "李四", "fruits" : [ "banana", "apple" ] }
{ "_id" : "1003", "name" : "王五", "fruits" : [ "banana", "apple", "orange" ] }
下面我们查询水果包含orange, apple的成员:
db.test.find({ "fruits": {$all: [ "orange", "apple" ]}});
此查询等效于:
db.test.find(
{
$and: [
{ "fruits": "orange" },
{ "fruits": "apple" }
]
}
);
查询的结果如下:
{ "_id" : "1001", "name" : "张三", "fruits" : [ "apple", "orange" ] }
{ "_id" : "1003", "name" : "王五", "fruits" : [ "banana", "apple", "orange" ] }
语法:{
例子:
下面我们查询水果包含orange的成员:
db.test.find(
{
"fruits": {
$elemMatch: { $eq: "orange" }
}
}
);
查询的结果如下:
{ "_id" : "1001", "name" : "张三", "fruits" : [ "apple", "orange" ] }
{ "_id" : "1003", "name" : "王五", "fruits" : [ "banana", "apple", "orange" ] }
语法:{
例子:
下面我们查询水果长度为2的成员:
db.test.find({ "fruits": { $size: 2 } });
查询的结果如下:
{ "_id" : "1001", "name" : "张三", "fruits" : [ "apple", "orange" ] }
{ "_id" : "1002", "name" : "李四", "fruits" : [ "banana", "apple" ] }