0

Consider the below collection. We have many data with the same pattern in the collection. All I need is to get all the distinct key name (i.e Com_stmt_execute, Table_locks_waited) excluding the actual data. I tried using .distinct() but I am not sure what to pass when there is no argument to give.

_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"
fear_matrix
  • 4,912
  • 10
  • 44
  • 65

1 Answers1

0

This can be done by using simple javascript code at mongo shell, have a look into it

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"
]

Another approach which will also filter the any duplicate keys is by using Set instead off simple array

> 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

Hope above solution will help you out.

krishna Prasad
  • 3,541
  • 1
  • 34
  • 44