Jonathan Jonathan - 2 months ago 12
Dart Question

How to get execution time on mongoDb with Dart?

How can I get the execution time of mongodb queries in Dart?

I am using https://github.com/mongo-dart/mongo_dart

Answer

I managed to call setProfilingLevel (copying what is done for drop()). Note that it is a system command

DbCommand createSetProfilingLevelCommand(int level) {
  return new DbCommand(
      db,
      DbCommand.SYSTEM_COMMAND_COLLECTION,
      MongoQueryMessage.OPTS_NO_CURSOR_TIMEOUT,
      0,
      -1,
      {'profile':level},
      null);
}
print(await db.executeDbCommand(createSetProfilingLevelCommand(2)));

It should display something like that

{was: 0, slowms: 100, ok: 1.0}

Querying the collection is the same that you did (assuming you know how to use it!)

var coll = db.collection(DbCommand.SYSTEM_PROFILE_COLLECTION);
List list = await coll.find().toList();
for (var item in list) {
  print(item);
}