Exemplo n.º 1
0
        public ICollection <BsonDocument> Execute()
        {
            var outputType = _pipelineStageDefinition.OutputType;

            if (outputType == typeof(BsonDocument))
            {
                var serializer     = BsonSerializer.SerializerRegistry.GetSerializer <BsonDocument>();
                var renderedStage  = _pipelineStageDefinition.Render(serializer, BsonSerializer.SerializerRegistry);
                var matchCondition = renderedStage.Document.GetElement(_pipelineStageDefinition.OperatorName).Value;
                FilterDefinition <BsonDocument> filter = matchCondition.ToString();
                var filterBson   = filter.Render(serializer, BsonSerializer.SerializerRegistry);
                var parsedFilter = FilterParser.Instance.Parse(filterBson);
                return(new BsonDocumentCollection(_documents.Where(parsedFilter.Filter)));
            }
            else
            {
                var renderedStage  = _pipelineStageDefinition.Render(_bsonSerializer, BsonSerializer.SerializerRegistry);
                var matchCondition = renderedStage.Document.GetElement(_pipelineStageDefinition.OperatorName).Value;
                FilterDefinition <TDocument> filter = matchCondition.ToString();
                var serializer   = BsonSerializer.SerializerRegistry.GetSerializer <TDocument>();
                var filterBson   = filter.Render(serializer, BsonSerializer.SerializerRegistry);
                var parsedFilter = FilterParser.Instance.Parse(filterBson);
                return(new BsonDocumentCollection(_documents.Where(parsedFilter.Filter)));
            }
        }
Exemplo n.º 2
0
        public ICollection <BsonDocument> Execute()
        {
            var serializer    = BsonSerializer.SerializerRegistry.GetSerializer <BsonDocument>();
            var renderedStage = _pipelineStageDefinition.Render(serializer, BsonSerializer.SerializerRegistry);
            var projections   = renderedStage.Document.GetElement(renderedStage.OperatorName).Value.AsBsonDocument;

            ICollection <BsonDocument> result = new BsonDocumentCollection();

            foreach (var document in _documents)
            {
                BsonDocument projectedDocument = new BsonDocument();
                for (int i = 0; i < projections.Names.Count(); i++)
                {
                    var whatToTake = projections.Values.ElementAt(i);
                    if (whatToTake.IsString && whatToTake.AsString.StartsWith("$"))
                    {
                        var projectedValue = document.Find(whatToTake.AsString.Remove(0, 1));
                        projectedDocument.Add(projections.Names.ElementAt(i), projectedValue);
                    }
                    else
                    {
                        projectedDocument.Add(projections.Names.ElementAt(i), whatToTake.AsBsonValue);
                    }
                }
                result.Add(projectedDocument);
            }

            return(result);
        }
Exemplo n.º 3
0
        public ICollection <BsonDocument> Execute()
        {
            ICollection <BsonDocument> result = new BsonDocumentCollection();
            var serializer    = BsonSerializer.SerializerRegistry.GetSerializer(_pipelineStageDefinition.OutputType);
            var renderedStage = _pipelineStageDefinition.Render(serializer, BsonSerializer.SerializerRegistry);
            var limit         = renderedStage.Document.GetElement(renderedStage.OperatorName).Value.AsInt32;

            foreach (var document in _documents.Take(limit))
            {
                result.Add(document);
            }
            return(result);
        }
Exemplo n.º 4
0
        public ICollection <BsonDocument> Execute()
        {
            ICollection <BsonDocument> result = new BsonDocumentCollection();
            var renderedStage  = _pipelineStageDefinition.Render(_bsonSerializer, BsonSerializer.SerializerRegistry);
            var fieldForUnwind = renderedStage.Document.GetElement("$unwind").Value.AsString.Remove(0, 1);

            foreach (var document in _documents)
            {
                if (document.AsBsonDocument.Contains(fieldForUnwind) && document.AsBsonDocument[fieldForUnwind].IsBsonArray)
                {
                    for (int i = 0; i < document.AsBsonDocument[fieldForUnwind].AsBsonArray.Count; i++)
                    {
                        var unwindedDocument = new BsonDocument(document.AsBsonDocument);
                        unwindedDocument[fieldForUnwind] = unwindedDocument[fieldForUnwind][i];
                        result.Add(unwindedDocument.AsBsonDocument);
                    }
                }
            }
            return(result);
        }