public static void p5_mongo_find(ApplicationContext context, ActiveEventArgs e) { // House cleaning using (new Utilities.ArgsRemover(e.Args, true)) { // Retrieving table name and running sanity check var tableName = e.Args.Get <string> (context); if (string.IsNullOrEmpty(tableName)) { throw new LambdaException("No table name supplied to [p5.mongo.find]", e.Args, context); } // Retrieving filter, defaulting to empty var filter = Filter.CreateFilter(context, e.Args); // Retrieving collection var collection = Database.Instance.MongoDatabase.GetCollection <BsonDocument> (tableName); // Running query var cursor = collection.Find(filter); // Sorting query cursor = SortCursor(context, e.Args, cursor); // Checking if we've got a [start] offset if (e.Args ["start"] != null) { cursor.Skip(e.Args.GetChildValue("start", context, 0)); } // Applying [count] int count = e.Args.GetChildValue("count", context, -1); // Looping through result set until [count] is reached int idxCount = 0; foreach (var idxDoc in cursor.ToEnumerable()) { // Making sure we return currently iterated document to caller var id = BsonTypeMapper.MapToDotNetValue(idxDoc.Elements.First(ix => ix.Name == "_id").Value); var idxNode = e.Args.Add(tableName, id).LastChild; // Parsing document, and stuffing results into idxNode, making sure we skip the "_id" element for main document DocumentParser.ParseDocument(context, idxNode, idxDoc, "_id"); // Checking if we've reached [count] if (++idxCount == count) { break; } } } }
public static void p5_mongo_aggregate(ApplicationContext context, ActiveEventArgs e) { // House cleaning using (new Utilities.ArgsRemover(e.Args, true)) { // Retrieving table name and running sanity check var tableName = e.Args.Get <string> (context); if (string.IsNullOrEmpty(tableName)) { throw new LambdaException("No table name supplied to [p5.mongo.find]", e.Args, context); } // Retrieving collection var collection = Database.Instance.MongoDatabase.GetCollection <BsonDocument> (tableName); // Retrieving grouping clauses and creating BsonDocument for Group method BsonDocument doc = new BsonDocument(); doc.Add("_id", BsonValue.Create(e.Args ["_id"].Value)); foreach (var idxChild in e.Args.Children.Where(ix => ix.Name != "_id" && ix.Name != "where")) { doc.Add(idxChild.Name, new BsonDocument(idxChild.Get <string> (context), 1)); } // Retrieving filter criteria FilterDefinition <BsonDocument> filter = null; if (e.Args ["where"] != null && e.Args ["where"].Children.Count > 0) { filter = Filter.CreateFilter(context, e.Args); } // Running query var aggregate = filter == null? collection.Aggregate().Group(doc) : collection.Aggregate().Match(filter).Group(doc); // Looping through each result foreach (var idxDoc in aggregate.ToEnumerable()) { // Making sure we return currently iterated document to caller var id = BsonTypeMapper.MapToDotNetValue(idxDoc.Elements.First(ix => ix.Name == "_id").Value); var idxNode = e.Args.Add(tableName, id).LastChild; // Parsing document, and stuffing results into idxNode, making sure we skip the "_id" element for main document DocumentParser.ParseDocument(context, idxNode, idxDoc, "_id"); } } }
public static void p5_mongo_list_indexes(ApplicationContext context, ActiveEventArgs e) { // Retrieving table name and running sanity check var tableName = e.Args.Get <string> (context); if (string.IsNullOrEmpty(tableName)) { throw new LambdaException("No table name supplied to [p5.mongo.list-indexes]", e.Args, context); } // Retrieving collection var collection = Database.Instance.MongoDatabase.GetCollection <BsonDocument> (tableName); // Looping through each index, and returning to caller foreach (var idxIndexCursor in collection.Indexes.List().ToEnumerable()) { // Making sure we return currently iterated document to caller var id = BsonTypeMapper.MapToDotNetValue(idxIndexCursor.Elements.First(ix => ix.Name == "name").Value).ToString(); var idxNode = e.Args.Add(id).LastChild; DocumentParser.ParseDocument(context, idxNode, idxIndexCursor, "name"); } }