具有Mongoose限制子文档的MongoDB [英] MongoDB with Mongoose limit subdocuments

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

问题描述

在MongoDB和Mongoose方面,我是个菜鸟,请原谅我...

I am a noob when it comes to MongoDB and Mongoose so please forgive me...

我正在将Node与Express和Mongoose一起使用.我有一个包含大量子文档的文档,以至于当我尝试加载所有子文档时,我的服务器内存不足.因此,我想选择一个子文档的最后30个项目.这是我现在所拥有的,然后是我想我想要的东西的猜测...

I am using Node with Express along with Mongoose. I have a document with a ton of subdocuments, so much so that my server runs out of memory when trying to load all the subdocuments. So I want to select the last 30 items of a subdocument. Here is what I have now, and then a guess as to what I think I want...

Device.findOne({ device_id: deviceId }, (err, device) => {
        ....
})

这是个猜测

Device.findOne({ device_id: deviceId }, { movements: { $slice: 30 } }, (err, device) => {
        ....
})

任何帮助都会很棒,在此先谢谢您!

Any help would be awesome, thanks in advance!!!

推荐答案

您几乎钉牢了它.

要获取最后30个值,只需使用减号即可.就您而言,您可以执行以下操作:

To get the last 30 values, just use minus instead. In your case, you can do something like:

Device.findOne({ device_id: deviceId }, { movements: { $slice: -30 } }, (err, device) => {
        ....
})

希望有帮助.

这篇关于具有Mongoose限制子文档的MongoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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