获取集合中所有键的名称

本文翻译自:Get names of all keys in the collection

I'd like to get the names of all the keys in a MongoDB collection. 我想获取MongoDB集合中所有键的名称。

For example, from this: 例如,从此:

db.things.insert( { type : ['dog', 'cat'] } );
db.things.insert( { egg : ['cat'] } );
db.things.insert( { type : [] } );
db.things.insert( { hello : []  } );

I'd like to get the unique keys: 我想获得唯一的键:

type, egg, hello

#1楼

参考:https://stackoom.com/question/9e2Y/获取集合中所有键的名称


#2楼

克里斯蒂娜的答案为灵感,我创建了一个名为Variety的开源工具,该工具正是这样做的: https : //github.com/variety/variety


#3楼

Try this: 尝试这个:

doc=db.thinks.findOne();
for (key in doc) print(key);

#4楼

I have 1 simpler work around... 我有1个更简单的解决方法...

What you can do is while inserting data/document into your main collection "things" you must insert the attributes in 1 separate collection lets say "things_attributes". 您可以做的是在将数据/文档插入主集合“事物”时,必须在1个单独的集合中插入属性,例如“ things_attributes”。

so every time you insert in "things", you do get from "things_attributes" compare values of that document with your new document keys if any new key present append it in that document and again re-insert it. 因此,每次插入“事物”时,您都会从“ things_attributes”中获得该文档的值与新文档密钥的比较,如果存在任何新密钥,则将该文档附加到该文档中,然后再次将其重新插入。

So things_attributes will have only 1 document of unique keys which you can easily get when ever you require by using findOne() 因此Things_attributes只有1个唯一键文档,您可以通过使用findOne()在需要时轻松获得


#5楼

I extended Carlos LM's solution a bit so it's more detailed. 我扩展了Carlos LM的解决方案,因此更加详细。

Example of a schema: 模式示例:

var schema = {
    _id: 123,
    id: 12,
    t: 'title',
    p: 4.5,
    ls: [{
            l: 'lemma',
            p: {
                pp: 8.9
            }
        },
         {
            l: 'lemma2',
            p: {
               pp: 8.3
           }
        }
    ]
};

Type into the console: 输入控制台:

var schemafy = function(schema, i, limit) {
    var i = (typeof i !== 'undefined') ? i : 1;
    var limit = (typeof limit !== 'undefined') ? limit : false;
    var type = '';
    var array = false;

    for (key in schema) {
        type = typeof schema[key];
        array = (schema[key] instanceof Array) ? true : false;

        if (type === 'object') {
            print(Array(i).join('    ') + key+' <'+((array) ? 'array' : type)+'>:');
            schemafy(schema[key], i+1, array);
        } else {
            print(Array(i).join('    ') + key+' <'+type+'>');
        }

        if (limit) {
            break;
        }
    }
}

Run: 跑:

schemafy(db.collection.findOne());

Output 输出量

_id <number>
id <number>
t <string>
p <number>
ls <object>:
    0 <object>:
    l <string>
    p <object>:
        pp <number> 

#6楼

You could do this with MapReduce: 您可以使用MapReduce做到这一点:

mr = db.runCommand({
  "mapreduce" : "my_collection",
  "map" : function() {
    for (var key in this) { emit(key, null); }
  },
  "reduce" : function(key, stuff) { return null; }, 
  "out": "my_collection" + "_keys"
})

Then run distinct on the resulting collection so as to find all the keys: 然后在结果集合上进行非重复运行,以便找到所有键:

db[mr.result].distinct("_id")
["foo", "bar", "baz", "_id", ...]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值