使用MongoDB限制字段中的值数 [英] Limit number of values in a field using MongoDB

查看:113
本文介绍了使用MongoDB限制字段中的值数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的收藏有一个值数组,但我只想检索最后一个X. 示例:

My collection has an array of values, but I'd like to retrieve only the last X. Example:

db.collection.find({
   "array": [0,1,2,3,4,5]
})

我想查询最后3个.我将如何处理?

And I'd like to query the last 3. How would I go about that?

我正在使用MongoDB,这是为Meteor应用程序设计的.

I'm using MongoDB and this is meant for a Meteor application.

推荐答案

您需要使用

You need to use the $slice operator.

db.collection.find( { /* filter */ }, { "array": { "$slice": -3 } } );

这篇关于使用MongoDB限制字段中的值数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆