NodeJS/MongoDB:对数组字段执行切片操作 [英] NodeJS/MongoDB: Perform Slice operation on an array field
本文介绍了NodeJS/MongoDB:对数组字段执行切片操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在尝试对数组执行切片时遇到了困难我有一个名为 comments 的集合,每个文档都有一个数组字段,我想访问它并应用切片进行分页,请帮忙!!尝试了 monk
和 mongodb
不好
I am having a tough time trying to execute a slice on an array
I have a collection called comments, each document has an array field, i want to access it and apply slice for paging purpose please help !! tried monk
and mongodb
no good
示例:
{
_id:xyz,
msgs:[{.....},{.....},{.....}]
}
database.collection("comments")
.find({"_id": id},{ "msgs": { "$slice": [2,5] } })
.toArray( function(err, result){
//implementation
});
推荐答案
对于 mongodb
驱动程序:
For mongodb
Driver:
代替
.find({"_id": id},{ "msgs": { "$slice": [2,5] } })
使用
.find({"_id": id}).project({ "msgs": { "$slice": [2,5] } })
如给定此处$slice
运算符属于 投影 类型.NodeJS 的 mongodb
驱动程序需要调用额外的 .project
函数来投影选择性字段以及使用投影运算符.
As given here the $slice
operator is of Projection type.
The mongodb
driver for NodeJS needs call to an extra .project
function to project selective fields as well as to use projection operators.
这篇关于NodeJS/MongoDB:对数组字段执行切片操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文