数组表达式运算符主要用于文档中数组的操作,本篇我们主要介绍数组表达式运算符中用于截取数组的操作,下面我们进行详细介绍:
{ $slice: [
或者 { $slice: [
获取数组中指定范围内的元素组成一个新的数组
初始化成员数据
db.persons.insertMany([
{ "_id" : "1001", "name" : "张三", "fruits" : [ "apple", "pineapple", "orange" ] },
{ "_id" : "1002", "name" : "李四", "fruits" : [ "banana", "apple", "pineapple" ] },
{ "_id" : "1003", "name" : "王五", "fruits" : [ "banana", "apple", "orange", "watermelon" ] },
{ "_id" : "1004", "name" : "赵六", "fruits" : [ ] },
{ "_id" : "1005", "name" : "田七" },
])
例子1:找到每个人最喜欢吃的前两个水果
db.persons.aggregate([
{
$project: {
"name": 1,
"favoriteFruits": { $slice: [ "$fruits", 2 ] }
}
}
])
聚合查询的结果如下:
{ "_id" : "1001", "name" : "张三", "favoriteFruits" : [ "apple", "pineapple" ] }
{ "_id" : "1002", "name" : "李四", "favoriteFruits" : [ "banana", "apple" ] }
{ "_id" : "1003", "name" : "王五", "favoriteFruits" : [ "banana", "apple" ] }
{ "_id" : "1004", "name" : "赵六", "favoriteFruits" : [ ] }
{ "_id" : "1005", "name" : "田七", "favoriteFruits" : null }
例子2:找到每个人最喜欢吃的最后两个水果
db.persons.aggregate([
{
$project: {
"name": 1,
"favoriteFruits": { $slice: [ "$fruits", -2 ] }
}
}
])
聚合查询的结果如下:
{ "_id" : "1001", "name" : "张三", "favoriteFruits" : [ "pineapple", "orange" ] }
{ "_id" : "1002", "name" : "李四", "favoriteFruits" : [ "apple", "pineapple" ] }
{ "_id" : "1003", "name" : "王五", "favoriteFruits" : [ "orange", "watermelon" ] }
{ "_id" : "1004", "name" : "赵六", "favoriteFruits" : [ ] }
{ "_id" : "1005", "name" : "田七", "favoriteFruits" : null }
(1)字段未定义时,结果为null;
(2)未找到元素,结果为[ ]
(3){ $slice: [
, ] } 语法中 n为正数,代表的是从前往后取N个元素
n为负数,代表的是从后往前取N个元素
例子3:找到每个人最喜欢吃的第二个水果
db.persons.aggregate([
{
$project: {
"name": 1,
"favoriteFruits": { $slice: [ "$fruits", 1, 1 ] }
}
}
])
聚合查询的结果如下:
{ "_id" : "1001", "name" : "张三", "favoriteFruits" : [ "pineapple" ] }
{ "_id" : "1002", "name" : "李四", "favoriteFruits" : [ "apple" ] }
{ "_id" : "1003", "name" : "王五", "favoriteFruits" : [ "apple" ] }
{ "_id" : "1004", "name" : "赵六", "favoriteFruits" : [ ] }
{ "_id" : "1005", "name" : "田七", "favoriteFruits" : null }
例子4:假如position为负数,会如何处理
db.persons.aggregate([
{
$project: {
"name": 1,
"favoriteFruits": { $slice: [ "$fruits", -2, 1 ] }
}
}
])
聚合查询的结果如下:
{ "_id" : "1001", "name" : "张三", "favoriteFruits" : [ "pineapple" ] }
{ "_id" : "1002", "name" : "李四", "favoriteFruits" : [ "apple" ] }
{ "_id" : "1003", "name" : "王五", "favoriteFruits" : [ "orange" ] }
{ "_id" : "1004", "name" : "赵六", "favoriteFruits" : [ ] }
{ "_id" : "1005", "name" : "田七", "favoriteFruits" : null }
例子5:假如position = -10,会如何处理
db.persons.aggregate([
{
$project: {
"name": 1,
"favoriteFruits": { $slice: [ "$fruits", -10, 1 ] }
}
}
])
聚合查询的结果如下:
{ "_id" : "1001", "name" : "张三", "favoriteFruits" : [ "apple" ] }
{ "_id" : "1002", "name" : "李四", "favoriteFruits" : [ "banana" ] }
{ "_id" : "1003", "name" : "王五", "favoriteFruits" : [ "banana" ] }
{ "_id" : "1004", "name" : "赵六", "favoriteFruits" : [ ] }
{ "_id" : "1005", "name" : "田七", "favoriteFruits" : null }
如果position为负数,则从后往前确定起始位置,当向前移动的位置超过了数组长度,则起始位置为0;然后再从前往后获取N个元素组成新的数组。