Пример #1
0
        private void ApplySorting(MongoCursor <BsonDocument> cursor, IEnumerable <SimpleOrderByItem> orderings)
        {
            if (orderings == null || !orderings.Any())
            {
                return;
            }

            var sortBuilder = new SortByBuilder();

            foreach (var ordering in orderings)
            {
                var name = ExpressionFormatter.GetFullName(ordering.Reference);
                if (ordering.Direction == OrderByDirection.Ascending)
                {
                    sortBuilder.Ascending(name);
                }
                else
                {
                    sortBuilder.Descending(name);
                }
            }

            cursor.SetSortOrder(sortBuilder);
        }
Пример #2
0
        public IEnumerable <IDictionary <string, object> > Find(MongoCollection <BsonDocument> collection, SimpleQuery query, out IEnumerable <SimpleQueryClauseBase> unhandledClauses)
        {
            var builder = MongoQueryBuilder.BuildFrom(query);

            unhandledClauses = builder.UnprocessedClauses;

            if (builder.IsTotalCountQuery)
            {
                long count;
                if (builder.Criteria == null)
                {
                    count = collection.Count();
                }
                else
                {
                    count = collection.Count(_expressionFormatter.Format(builder.Criteria));
                }

                //TODO: figure out how to make count a long
                builder.SetTotalCount((int)count);
            }

            if (!builder.SkipCount.HasValue && builder.TakeCount.HasValue && builder.TakeCount.Value == 1)
            {
                return new[] { FindOne(collection, builder.Criteria) }
            }
            ;

            var cursor = CreateCursor(collection, builder.Criteria);

            ApplyFields(cursor, builder.Columns);
            ApplySorting(cursor, builder.Order);
            ApplySkip(cursor, builder.SkipCount);
            ApplyTake(cursor, builder.TakeCount);

            var aliases = builder.Columns.OfType <ObjectReference>().ToDictionary(x => ExpressionFormatter.GetFullName(x), x => x.GetAlias());

            return(cursor.Select(x => x.ToSimpleDictionary(aliases)));
        }