Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何筛选出与mongodb中$slice相同的密钥_Node.js_Mongodb_Mongojs - Fatal编程技术网

Node.js 如何筛选出与mongodb中$slice相同的密钥

Node.js 如何筛选出与mongodb中$slice相同的密钥,node.js,mongodb,mongojs,Node.js,Mongodb,Mongojs,我有这样一个收藏: [ {_id: "1234", myId: 1, a: [1,2,3], b: [23,3,2], c: [3,234,4], ...}, {_id: "5678", myId: 2, a: [21,32,3], b: [32,123,4], c: [32,32,12], ...}, {_id: "3242", myId: 3, a: [21,23,2], b: [12,2,32], c: [12,213,1], ...} ] 每个文档中都有更多的数组,而且每个数组的大小都

我有这样一个收藏:

[
{_id: "1234", myId: 1, a: [1,2,3], b: [23,3,2], c: [3,234,4], ...},
{_id: "5678", myId: 2, a: [21,32,3], b: [32,123,4], c: [32,32,12], ...},
{_id: "3242", myId: 3, a: [21,23,2], b: [12,2,32], c: [12,213,1], ...}
]
每个文档中都有更多的数组,而且每个数组的大小都要大得多。我希望能够在我想要的两个键上应用$slice投影来检索50个最新的值,并且只返回这两个键,使用

我知道如何分别完成这些任务,但我无法找出两者的交叉点

因此,在find函数中使用{a:{$slice:-50},b:{$slice:-50}作为投影将返回最后50个条目,{a:1,b:1}将只返回find结果中的这两个键


如何同时执行这两项操作?

您可以尝试在投影中添加虚拟字段:

db.test.find({}, {a: {$slice: -2}, b: {$slice: -2}, dummy: 1, _id: 0})
返回


你能再解释一下这个傻瓜吗:1?你为什么要投投影?有关于dummy的文档链接吗?@yogesh这是一种变通方法,您可以投影一个不存在的字段,它可以是任何名称,只要它不是投影数组旁边的文档架构的一部分。还没有这方面的文档。非常感谢!工作得很有魅力!
/* 0 */
{
    "a" : [ 2, 3 ],
    "b" : [ 3, 2 ]
}

/* 1 */
{
    "a" : [ 32, 3 ],
    "b" : [ 123, 4 ]
}

/* 2 */
{
    "a" : [ 23, 2 ],
    "b" : [ 2, 32 ]
}