Example #1
0
        private static List <BsonDocument> BuildPipeline(BsonValue initialMatch, int skip, int limit, OrderDirection order)
        {
            var pipeline = new List <BsonDocument> {
                new BsonDocument {
                    { "$match", initialMatch }
                },
            };

            if (order != OrderDirection.None)
            {
                var tSort = new BsonDocument {
                    { "Timestamp", order.ToInt() }
                };

                var sort = new BsonDocument {
                    { "$sort", tSort }
                };

                pipeline.Add(sort);
            }

            if (skip > 0)
            {
                pipeline.Add(new BsonDocument {
                    { "$skip", skip }
                });
            }

            if (limit > 0)
            {
                pipeline.Add(new BsonDocument {
                    { "$limit", limit }
                });
            }



            return(pipeline);
        }
        private static List <BsonDocument> BuildPipeline(BsonValue initialMatch, DateTime start, DateTime end, int skip, int limit, OrderDirection order)
        {
            var projectRewrite = new BsonDocument {
                { "_id", 1 },
                { "SensorId", 1 },
                { "Timestamp", "$Measurements.Timestamp" },
                { "Location", "$Measurements.Location" },
                { "Data", "$Measurements.Data" },
            };

            var timestampMatch = new BsonDocument {
                {
                    "Timestamp", new BsonDocument {
                        { "$gte", start },
                        { "$lt", end }
                    }
                }
            };

            var pipeline = new List <BsonDocument> {
                new BsonDocument {
                    { "$match", initialMatch }
                },
                new BsonDocument {
                    { "$unwind", "$Measurements" }
                },
                new BsonDocument {
                    { "$project", projectRewrite }
                },
                new BsonDocument {
                    { "$match", timestampMatch }
                },
            };

            if (order != OrderDirection.None)
            {
                var tSort = new BsonDocument {
                    { "Timestamp", order.ToInt() }
                };

                var sort = new BsonDocument {
                    { "$sort", tSort }
                };

                pipeline.Add(sort);
            }

            if (skip > 0)
            {
                pipeline.Add(new BsonDocument {
                    { "$skip", skip }
                });
            }

            if (limit > 0)
            {
                pipeline.Add(new BsonDocument {
                    { "$limit", limit }
                });
            }



            return(pipeline);
        }