发布于 2015-09-14 15:09:50 | 209 次阅读 | 评论: 0 | 来源: 网络整理

$slice

The $slice operator controls the number of items of an array that a query returns. Consider the following prototype query:

db.collection.find( { field: value }, { array: {$slice: count } } );

This operation selects the document collection identified by a field named field that holds value and returns the number of elements specified by the value of count from the array stored in the array field. If count has a value greater than the number of elements in array the query returns all elements of the array.

$slice accepts arguments in a number of formats, including negative values and arrays. Consider the following examples:

db.posts.find( {}, { comments: { $slice: 5 } } )

Here, $slice selects the first five items in an array in the comments field.

db.posts.find( {}, { comments: { $slice: -5 } } )

This operation returns the last five items in array.

The following examples specify an array as an argument to slice. Arrays take the form of [ skip , limit ], where the first value indicates the number of items in the array to skip and the second value indicates the number of items to return.

db.posts.find( {}, { comments: { $slice: [ 20, 10 ] } } )

Here, the query will only return 10 items, after skipping the first 20 items of that array.

db.posts.find( {}, { comments: { $slice: [ -20, 10 ] } } )

This operation returns 10 items as well, beginning with the item that is 20th from the last item of the array.

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务