我如何从mongodb / pymongo中获得不同的密钥

时间:2019-04-15 12:34:18

标签: mongodb pymongo

考虑以下集合。集合中有许多具有相同模式的数据。我需要的是获取所有不包含实际数据的唯一键名(即Com_stmt_execute,Table_locks_waited)。我尝试使用.distinct(),但不确定在没有参数可传递时要传递什么。

_id:5ca358c762fbaff4c7fd53c3
Com_stmt_execute:85684007
Table_locks_waited:0
Handler_rollback:1339763
Bytes_sent:57938711178278
Threads_running:6
Innodb_data_reads:559270903
Threads_connected:2561
Open_tables:6691
Host:"172.16.3.111"
Date:"2019-04-02T18:12:47"
Bytes_received:10945552148828
Handler_commit:8909267294
Name:"EmiratesMasterDB"

1 个答案:

答案 0 :(得分:0)

这可以通过在 mongo shell 中使用简单的javascript代码来完成,看看

var values = db.collection.find();
var keys = [];
values.forEach(function(o) {  for (var k in o ) keys.push(k);  })
> keys // Check the all possible keys in the collection
[
    "_id",
    "Com_stmt_execute",
    "Table_locks_waited",
    "Handler_rollback",
    "Bytes_sent",
    "Threads_running",
    "Innodb_data_reads",
    "Threads_connected",
    "Open_tables",
    "Host",
    "Date",
    "Bytes_received",
    "Handler_commit",
    "Name"
]

另一种过滤所有重复键的方法是使用Set而不是简单数组

> var dataset = db.collection.find();
> const keysHolder = new Set();
> dataset.forEach( function (o) {for (key in o ) keysHolder.add(key)})
> for(let item of keysHolder) {print(item)}
_id
Com_stmt_execute
Table_locks_waited
Handler_rollback
Bytes_sent
Threads_running
Innodb_data_reads
Threads_connected
Open_tables
Host
Date
Bytes_received
Handler_commit
Name

希望以上解决方案能够为您提供帮助。