MongoDB 查询文档(3)

我们之前讲解过,查询文档的语法:

db.collection.find(query, projection, options)

其中 query 代表的是查询过滤器,projection 代表的是文档返回的字段,options 代表的是用于查询的其他选项;

我们已经对query进行了详细的介绍,具体可以参考:

MongoDB 查询文档(1)https://blog.csdn.net/m1729339749/article/details/129965699MongoDB查询文档(2)https://blog.csdn.net/m1729339749/article/details/129971708本篇我们主要介绍 projection(投影)

一、返回指定字段

语法:{ : 1, : 1, ......, : 1}

例子:

查询集合test所有的文档:

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

下面我们查询所有文档,只返回name字段:

db.test.find({}, {"name": 1});

查询的结果如下:

{ "_id" : "1001", "name" : "张三" }
{ "_id" : "1002", "name" : "李四" }
{ "_id" : "1003", "name" : "王五" }

【注意】_id是必须返回的

二、投影数组中满足条件的第一个元素($)

语法:{ : 1 }

此语法用于返回数组中满足条件的第一个元素

例子:

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

db.test.find(
    { 
        "fruits": {
            $all: [ "orange", "apple" ]
        } 
    }
);

查询的结果如下:

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

下面我们加上返回的字段:

db.test.find(
    { 
        "fruits": {
            $all: [ "orange", "apple" ]
        } 
    },
    {
        "name": 1,
        "fruits.$": 1
    }
);

查询的结果如下:

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

从返回的结果我们可以看出,fruits中的数据是满足条件的第一个元素

三、投影数组中匹配$elemMatch条件的第一个元素($elemMatch)

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

例子:

下面查询所有的成员:

db.test.find({});

查询的结果如下:

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

下面我们加上返回的字段,并对返回的数组元素进行投影:

db.test.find({}, 
    {
        fruits: {
            $elemMatch: { $in: ["apple", "banana"] },
        },
        name: 1
    }
);

查询的结果如下:

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

从返回的结果我们可以看出,fruits中的数据是匹配$elemMatch条件的第一个元素

四、投影数组中的元素数($slice)

语法:{ : { $slice: } }

或 { : { $slice: [ , ] } }

$slice: :number为正数, 返回前number个元素

                               number为负数,返回后number个元素

                               如果大于数组的元素个数,则返回所的元素

$slice: [ , ]:跳过指定数量的元素后返回指定的元素数

例子:

查询前两个元素:

db.test.find({}, { fruits: { $slice: 2 } });

查询的结果如下:

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

查询后两个元素:

db.test.find({}, { fruits: { $slice: -2 } });

查询的结果如下:

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

跳过1个元素,取两个元素:

db.test.find({}, { fruits: { $slice: [1, 2] } });

查询的结果如下:

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

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