如何限制MongoDB中的子列数? [英] How to limit the number of sub column in MongoDB?

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

问题描述

我有一个类似的数据

{
 u'_id': {u'$oid': u'548e8396f7e19418857e47fc'},
 u'a': [
     {u'd': u'a'},
     {u'd': u'a'},
     ...,
     {u'd': u'a'}
 ]
}

我想知道如何限制子列的数量

I wonder how do I limit the number of sub column like

{u'_id': {u'$oid': u'548e8396f7e19418857e47fc'}, u'a': [{u'd': u'a'},{u'd': u'a'}, {u'd': u'a'}] }

{u'_id': {u'$oid': u'548e8396f7e19418857e47fc'}, u'a': [{u'd': u'a'}, {u'd': u'a'}, {u'd': u'a'}] }

对于整个文档,我可以使用

For the entire document, I can do it using

db.Col.find().limit(5)

db.Col.find().limit(5)

但是如何在每个文档中提取多个子列?

But how to extract several sub column at each document?

推荐答案

您可以使用 $slice 运算符.

You can use $slice operator.

你正在寻找的语法是这样的:

The syntax you are looking for is something like this:

db.your_collection.find({
   ...
}, {
   your_field: { $slice: 3 }
});

这应该从你的数组中返回 3 个第一个子元素(你可以使用 -3 返回最后一个)

this should return you 3 first subelements from your array (you can use -3 to return last)

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

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