public IActionResult QueryEditor(string query) { //https://docs.mongodb.com/manual/reference/command/ if (string.IsNullOrEmpty(query)) return ErrorForKendoGridJson("Empty query"); try { var result = _mongoDBContext.RunCommand<BsonDocument>(query); var ok = result.Where(x => x.Name == "ok").FirstOrDefault().Value.ToBoolean(); var gridModel = new DataSourceResult(); if (result.Where(x => x.Name == "cursor").ToList().Any()) { var resultCollection = result["cursor"]["firstBatch"].AsBsonArray.ToList(); var response = Serialize(resultCollection); gridModel = new DataSourceResult { Data = response, Total = response.Count() }; } else if (result.Where(x => x.Name == "n").ToList().Any()) { List<dynamic> n = new List<dynamic>(); var number = result["n"].ToInt64(); n.Add(new { Number = number }); gridModel = new DataSourceResult { Data = n }; } return Json(gridModel); } catch (Exception ex) { return ErrorForKendoGridJson(ex.Message); } }