查询 MongoDB 集合中所有不同字段的列表 [英] Querying for a list of all distinct fields in MongoDB collection

查看:186
本文介绍了查询 MongoDB 集合中所有不同字段的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个集合,其中存储了所有表单数据,每个表单具有不同的结构,但有些表单具有重叠的字段名称.我喜欢 mongo,因为它在这里证明了它的易用性,它允许我根据一些小的共同因素对完全不同的数据进行排序和聚合.

I have a collection where all form data is stored, each form with a different structure, but some forms with overlapping field names. I love mongo for the ease it proves here, allowing me to sort and aggregate totally different data based upon a few small common factors.

现在,对于 UI,我需要数据库中所有可能字段的列表,以便用户可以选择要查看的字段.除了索引,我似乎找不到任何 mongo 任何东西来返回字段列表.

Now, for the UI, I need a list of all possible fields in the database so a user can choose which fields to view. I can't seem to find any mongo anything to return a list of fields, except for indexes.

我该怎么做?

推荐答案

没有提供此信息的本机 MongoDB 功能.MongoDB 是完全无模式的,因此您必须执行完整的数据库遍历才能编译所有唯一字段名称的列表.

There is no native MongoDB functionality that provides this information. MongoDB is completely schemaless and as such you will have to do a full database walk to compile a list of all unique field names.

唯一可能的解决方法是使用 {field: <field name>, value: <field value>} 对存储所有字段并对字段"运行不同的操作或维护数据库中单独的唯一字段名称列表.两者都有明显的缺点.

The only possible workarounds are to store all fields using {field: <field name>, value: <field value>} pairs and run a distinct operation on "field" or to maintain a list of unique field names seperately in the database. Both have significant downsides.

这篇关于查询 MongoDB 集合中所有不同字段的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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