public IEnumerable<BsonDocument> Run(MongoCollection<Rental> rentals) { var priceRange = new BsonDocument( "$subtract", new BsonArray { "$Price", new BsonDocument( "$mod", new BsonArray{"$Price",500} ) }); var grouping =new BsonDocument( "$group", new BsonDocument { {"_id",priceRange}, {"count",new BsonDocument("$sum",1)} }); var sort=new BsonDocument( "$sort", new BsonDocument("_id",1) ); var args=new AggregateArgs { Pipeline=new []{grouping,sort} }; return rentals.Aggregate(args); }
// constructors public AggregateEnumerableResult( MongoCollection collection, AggregateArgs args, string outputCollectionName) { _collection = collection; _args = args; // TODO: make a defensive copy? _outputCollectionName = outputCollectionName; }
// constructors public AggregateEnumerableResult( MongoCollection collection, AggregateArgs args, string outputCollectionName, MessageEncoderSettings messageEncoderSettings) { _collection = collection; _args = args; // TODO: make a defensive copy? _outputCollectionName = outputCollectionName; _messageEncoderSettings = messageEncoderSettings; }
private void AggregateExamples() { AggregateArgs simpleAggregateArgs = new AggregateArgs() { Pipeline = new[] { new BsonDocument("$match", Query<Car>.LTE(c => c.DailyRentalFee, 10).ToBsonDocument()) , new BsonDocument("$match", Query<Car>.GTE(c => c.DailyRentalFee, 3).ToBsonDocument()) , new BsonDocument("$sort", new BsonDocument("DailyRentalFee", 0)) } }; IEnumerable<BsonDocument> documents = CarRentalContext.Cars.Aggregate(simpleAggregateArgs); }
public void MatchMatchSort() { InsertPeople(); var matchAndSort = new AggregateArgs { Pipeline = new[] { new BsonDocument("$match", Query.LT("Age", 70).ToBsonDocument()), new BsonDocument("$match", Query.GT("Age", 30).ToBsonDocument()), new BsonDocument("$sort", new BsonDocument("Age", 1)) } }; PrintResults(matchAndSort); }
public void MatchGroupSort() { InsertPeople(); var aggregation = new AggregateArgs { Pipeline = new[] { new BsonDocument("$match", Query.LT("Age", 70).ToBsonDocument()), new BsonDocument("$group", new BsonDocument { {"_id", new BsonDocument("$subtract", new BsonArray {"$Age", new BsonDocument("$mod", new BsonArray {"$Age", 10})})}, {"averageAge", new BsonDocument("$avg", "$Age")} }), new BsonDocument("$sort", new BsonDocument("_id", 1)) } }; PrintResults(aggregation); }
public void TestAggregateOutputToCollection() { if (_primary.Supports(FeatureId.AggregateOutputToCollection)) { _collection.RemoveAll(); _collection.DropAllIndexes(); _collection.Insert(new BsonDocument("x", 1)); _collection.Insert(new BsonDocument("x", 2)); _collection.Insert(new BsonDocument("x", 3)); _collection.Insert(new BsonDocument("x", 3)); var args = new AggregateArgs { Pipeline = new BsonDocument[] { new BsonDocument("$group", new BsonDocument { { "_id", "$x" }, { "count", new BsonDocument("$sum", 1) } }), new BsonDocument("$out", "temp") } }; var query = _collection.Aggregate(args); var results = query.ToList(); var dictionary = new Dictionary<int, int>(); foreach (var result in results) { var x = result["_id"].AsInt32; var count = result["count"].AsInt32; dictionary[x] = count; } Assert.AreEqual(3, dictionary.Count); Assert.AreEqual(1, dictionary[1]); Assert.AreEqual(1, dictionary[2]); Assert.AreEqual(2, dictionary[3]); } }
public void TestAggregateMaxTime() { if (_primary.Supports(FeatureId.MaxTime)) { using (var failpoint = new FailPoint(FailPointName.MaxTimeAlwaysTimeout, _server, _primary)) { if (failpoint.IsSupported()) { _collection.RemoveAll(); _collection.DropAllIndexes(); _collection.Insert(new BsonDocument("x", 1)); failpoint.SetAlwaysOn(); var args = new AggregateArgs { Pipeline = new BsonDocument[] { new BsonDocument("$match", Query.Exists("_id").ToBsonDocument()) }, MaxTime = TimeSpan.FromMilliseconds(1) }; var query = _collection.Aggregate(args); Assert.Throws<ExecutionTimeoutException>(() => query.ToList()); } } } }
public BlobStoreInfo GetInfo() { var aggregation = new AggregateArgs() { Pipeline = new[] { BsonDocument.Parse("{$group:{_id:1, size:{$sum:'$length'}, count:{$sum:1}}}") } }; var allInfos = _fs.Values .Select(x => x.Files.Aggregate(aggregation).FirstOrDefault()) .Where(x => x != null) .Select(x => new { size = x["size"].ToInt64(), files = x["count"].ToInt64() }) .ToArray(); return new BlobStoreInfo( allInfos.Sum(x => x.size), allInfos.Sum(x => x.files) ); }
private void PrintResults(AggregateArgs matchAndSort) { QueryTests.Aggregate(matchAndSort) .ToList() .ForEach(Console.WriteLine); }
public void TestAggregateWriteConcern() { RequireServer.Check().Supports(Feature.AggregateOut, Feature.CommandsThatWriteAcceptWriteConcern).ClusterType(ClusterType.ReplicaSet); var writeConcern = new WriteConcern(9); var args = new AggregateArgs { Pipeline = new[] { BsonDocument.Parse("{ $out : 'out' }") } }; var exception = Record.Exception(() => _collection.WithWriteConcern(writeConcern).Aggregate(args)); exception.Should().BeOfType<MongoWriteConcernException>(); }