php 网站出现乱码,做网站能赚流量钱吗,js网页设计案例,网站seo优化主要有哪些手段在投射中#xff0c;使用$操作符和$elemMatch返回数组中第一个符合查询条件的元素。而在投射中使用$slice, 能够返回指定数量的数组元素。
定义
投射中使用$slice命令#xff0c;指定查询结果中返回数组元素的数量。
语法
db.collection.find(query,{arrayFi…在投射中使用$操作符和$elemMatch返回数组中第一个符合查询条件的元素。而在投射中使用$slice, 能够返回指定数量的数组元素。
定义
投射中使用$slice命令指定查询结果中返回数组元素的数量。
语法
db.collection.find(query,{arrayField: {$slice: number to return}
)db.collection.find(query,{ arrayField: {$slice: [ number to skip, number to return]}}
)
其中number to return,指定了返回数组元素的数量当定义为正值时返回数组的前面几个元素而定义负值时返回数组后面的元素。如果定义的值大于数组长度则返回数组中所有的元素。
number to skip定义了自哪个元素开始返回数组元素。与number to return定义一致当定义正值时从数组开头跳过指定数量的数组元素。当定义负值时从数组第一个元素开始向后跳过指定数量的数组元素。这里number to skip的正数和负数只是指定起点自第一个元素开始跳转的方向和跳动的次数。无论正数还是负数使用number to return获取数组元素时都是从数组的正向开始获取。
行为
在投射定义查询结果包含字段时在内嵌文档的数组中使用$slice内嵌文档只会返回slice定义的字段。
构建包含下面数据的inventory集合其中details字段是文档类型details.colors和details.sizes是文档details中的两个数组元素。
db.inventory.insertOne(
{ item: socks, qty: 100, details: { colors: [ blue, red ], sizes: [ S, M, L] } }
)
使用$slice返回colors字段中的第一个数组元素
db.inventory.find({},{qty: 1, details.colors: { $slice: 1}}
)
返回结果
{_id : ObjectId(65b82e5b5943471b2e552d50),qty : 100,details : {colors : [ blue ]}
}
在投射定义查询结果不包含字段时在内嵌文档的数组中使用$slice 内嵌文档会返回其他字段。
db.inventory.find({},{_id: 0, details.colors: { $slice: 1}}
)
返回结果
{item : socks,qty : 100,details : {colors : [ blue ],sizes : [ S, M, L ]}
}
4.4版本前的数据库行为有差异无论是定义包含字段还是不包含字段投射中都会包含其他字段。
对视图的查询不支持$slice操作符。
4.4版本开始使用find, findAndModify定义查询语句的投射中不能将$slice应用到$操作符号中。下面的查询语句是错误的。
db.inventory.find({instock.qty: {$gt:25}}, {instock.$: {$slice:1}}
){message : positional projection cannot be used with an expression or sub object,ok : 0,code : 31271,codeName : Location31271
}
在文档数组字段使用$slice时需要注意路径冲突
向集合inventory插入文档。其中instock是文档数组类型的数据。
db.inventory.insertOne({ item: books, qty: 100, details: { colors: [ blue, red ], sizes: [ S, M, L] },instock: [{warehouse: A,qty:35},{warehouse: B,qty:15},{warehouse: C,qty:35}]}
)
构建针对instock的投射查询语句
db.inventory.find({},{ instock: {$slice:1}, instock.warehouse: 0})
因为instock和instock.warehouse定义的路径冲突所以此语句无效。
{message : Path collision at instock.warehouce remaining portion warehouce,ok : 0,code : 31249,codeName : Location31249
}
应用
构建测试集合。向posts集合中插入两条数据数据包含文档数组类型的字段comments.
db.posts.insertMany([{_id: 1,title: Bagels are not croissants.,comments: [ { comment: 0. true }, { comment: 1. croissants arent bagels.} ]},{_id: 2,title: Coffee please.,comments: [ { comment: 0. fooey }, { comment: 1. tea please }, { comment: 2. iced coffee }, { comment: 3. cappuccino }, { comment: 4. whatever } ]}
])
构建查询语句返回数组comments中前三个元素。
db.posts.find({}, {comments: {$slice: 3}})
返回数组comments中后面三个元素。
db.posts.find({}, {comments: {$slice: -3}})
正向跳过1个元素获取后面的三个元素
db.posts.find({}, {comments: {$slice: [1, 3]}})
反向跳过1个元素获取后面的三个元素
db.posts.find({}, {comments: {$slice: [-1, 3]}})//返回结果
/* 1 */
{_id : 1,title : Bagels are not croissants.,comments : [{comment : 1. croissants arent bagels.}]
},/* 2 */
{_id : 2,title : Coffee please.,comments : [{comment : 4. whatever}]
}
这个语句的返回结果就值得探讨了。正常理解可能会认为是跳过数组最后一个元素然后获取最后一个元素前面的三个元素即{ comment: 1. tea please }, { comment: 2. iced coffee }, { comment: 3. cappuccino }。但这样获取的方向就是-3而不是3了。在$slice中指定跳过几个元素时指定获取元素的数量只能是正数。也就表示获取元素的方向只能是正向。使用{$slice: [-1, 3]}时表示从数组第一个元素开始向后跳转一步到达第四个元素再以第四个元素作为起点获得后面的3个元素。因为第四个元素后面没有数据了所以只返回一个元素。