MongoDB 查询文档(2)

上一篇 MongoDB查询文档(1) 中介绍了MongoDB查询文档中使用比较筛选和逻辑筛选,这里我们继续介绍MongoDB的文档查询,这里我们介绍元素筛选、数组筛选。

一、元素筛选

1、判断元素是否存在($exists)

语法:{ 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" : "赵六" }

2、选择元素为指定类型的文档($type)

语法:{ 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" : "赵六" }

二、数组筛选

1、筛选数组字段中包含所有指定元素的文档($all)

语法:{ : { $all: [ , ... ] } }

例子:

查询集合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" ] }

2、筛选数组字段中至少有一个元素满足匹配条件的文档($elemMatch)

语法:{ : { $elemMatch: { , , ... } } }

例子:

下面我们查询水果包含orange的成员: 

db.test.find(
    { 
        "fruits": {
            $elemMatch: { $eq: "orange" } 
        } 
    }
);

查询的结果如下:

{ "_id" : "1001", "name" : "张三", "fruits" : [ "apple", "orange" ] }
{ "_id" : "1003", "name" : "王五", "fruits" : [ "banana", "apple", "orange" ] }

 3、筛选数字字段长度为指定大小的文档($size)

语法:{ : { $size: } }

例子:

下面我们查询水果长度为2的成员: 

db.test.find({ "fruits": { $size: 2 } });

查询的结果如下:

{ "_id" : "1001", "name" : "张三", "fruits" : [ "apple", "orange" ] }
{ "_id" : "1002", "name" : "李四", "fruits" : [ "banana", "apple" ] }

你可能感兴趣的:(MongoDB,mongodb,数据库,nosql)