コード例 #1
0
ファイル: MongoLog.cs プロジェクト: 24/source_04
 public static MongoCursor <TDocument> zFindAll <TDocument>(this MongoCollection collection, SortByWrapper sort = null, FieldsWrapper fields = null, int limit = 0, BsonDocument options = null)
 {
     MongoLog.CurrentMongoLog.LogFindAllAs(collection, sort, fields, limit, options);
     return(MongoLog.CurrentMongoLog.ExecuteAndLogResult(
                () =>
     {
         MongoCursor <TDocument> cursor = collection.FindAllAs <TDocument>();
         if (sort != null)
         {
             cursor.SetSortOrder(sort);
         }
         if (fields != null)
         {
             cursor.SetFields(fields);
         }
         if (limit != 0)
         {
             cursor.SetLimit(limit);
         }
         if (options != null)
         {
             cursor.SetOptions(options);
         }
         return cursor;
     }));
 }
コード例 #2
0
        /// <summary>
        /// 获取列表top几
        /// </summary>
        /// <param name="sortByMap"> var sort = new SortByDocument { { "_id", -1 } }; ->这里1为ASC, -1为DESC</param>
        /// <returns></returns>
        public List <T> List(Expression <Func <T, bool> > filter, Dictionary <string, sbyte> sortByMap = null, int?top = null)
        {
            MongoCursor <T> cursor = null;

            if (null != filter)
            {
                var findQuery = Query <T> .Where(filter);

                cursor = this._mongoCollection.Find(findQuery);
            }
            else
            {
                cursor = this._mongoCollection.FindAll();
            }

            if (null != sortByMap)
            {
                var orderByQuery = new SortByDocument(sortByMap);
                cursor = cursor.SetSortOrder(orderByQuery);
            }

            if (top.HasValue)
            {
                List <T> list = cursor.SetLimit(top.Value).ToList();
                return(list);
            }
            return(cursor.ToList());
        }
コード例 #3
0
 private void ApplyTake(MongoCursor <BsonDocument> cursor, int?takeCount)
 {
     if (takeCount.HasValue)
     {
         cursor.SetLimit(takeCount.Value);
     }
 }
コード例 #4
0
        /// <summary>
        /// 获取数据列表
        /// </summary>
        /// <typeparam name="T">数据类型</typeparam>
        /// <param name="DbName">数据库名称</param>
        /// <param name="TableName">表名</param>
        /// <param name="where">查询条件</param>
        /// <param name="orderby">排序</param>
        /// <param name="fields">返回字段</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">分页大小</param>
        /// <param name="count">记录总数</param>
        /// <returns>数据列表</returns>
        public static List <T> GetPaged <T>(string DbName, string TableName, BsonDocument where, string[] orderby, BsonDocument fields, int pageIndex, int pageSize)
        {
            List <T> list = new List <T>();

            try
            {
                MongoDatabase db = mongo.GetDatabase(DbName);//数据库名字
                if (db == null)
                {
                    throw new ArgumentNullException("db not found.");
                }
                MongoCollection <BsonDocument> categories = db.GetCollection <BsonDocument>(TableName);//表的名字
                MongoCursor <T> cursor = categories.FindAs <T>(new QueryComplete(where));
                cursor.SetSkip(((pageIndex - 1) * pageSize));
                cursor.SetLimit(pageSize);
                cursor.SetSortOrder(SortBy.Descending(orderby));
                foreach (T t in cursor)
                {
                    list.Add(t);
                }
            }
            catch (Exception ex)
            {
                LogService.Write(ex);
            }
            return(list);
        }
コード例 #5
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="U"></typeparam>
        /// <param name="_mongoCursor"></param>
        /// <returns></returns>
        public U GetOneItem <U>(MongoCursor _mongoCursor) where U : AbstractTGObject, new()
        {
            MongoCursor cursor = _mongoCursor.SetLimit(1);

            foreach (BsonDocument doc in cursor)
            {
                U obj = BsonHelper.Get <U>(doc);
                return(obj);
            }

            return(null);
        }
コード例 #6
0
ファイル: Test_mongodb_f.cs プロジェクト: 24/source_04
        public static void Test_MongoDB_FindAll_02 <T>(string collection)
        {
            Trace.WriteLine("Test_MongoDB_FindAll_02");
            MongoDatabase mdb = GetMongoDatabase();
            //MongoCollectionSettings<T> colSettings = new MongoCollectionSettings<T>(mdb, collection);
            //colSettings.ReadPreference.ReadPreferenceMode = ReadPreferenceMode.Nearest;
            //colSettings.WriteConcern = new WriteConcern();
            MongoCollection <T> mcollection = mdb.GetCollection <T>(collection);
            MongoCursor <T>     mcursor     = mcollection.FindAll();

            mcursor.SetLimit(10);
            //RunSource.CurrentRunSource.View(mcursor);
            mcursor.zView();
        }
コード例 #7
0
ファイル: MongoSortingUtils.cs プロジェクト: rotovibe/engage
        public static MongoCursor ApplySkipTake(MongoCursor mongoCursor, ISortableRequest dataRequest)
        {
            int take = MongoSortingUtils.GetTake(dataRequest);

            if (take > 0)
            {
                mongoCursor = mongoCursor.SetLimit(take);
            }
            int skip = MongoSortingUtils.GetSkip(dataRequest);

            if (skip > 0)
            {
                mongoCursor = mongoCursor.SetSkip(skip);
            }
            return(mongoCursor);
        }
コード例 #8
0
ファイル: MongoSession.cs プロジェクト: mark2k/mongoSession
        /// <summary>
        /// Perform a full text query on all fields indexed as text.
        /// </summary>
        /// <returns>First 50 results</returns>
        public List <T> TextSearch <T>(string searchStr, int skip, int limit, string orderBy) where T : class, new()
        {
            MongoCursor <T> cursor = mongoDb.GetCollection <T>(GetTypeName(typeof(T))).Find(Query.Text(searchStr));

            if (skip > 0)
            {
                cursor.SetSkip(skip);
            }

            cursor.SetLimit(limit);

            if (orderBy != null)
            {
                cursor.SetSortOrder(SortBy.Descending(orderBy));
            }

            return(cursor.ToList());
        }
コード例 #9
0
ファイル: MtgStore.cs プロジェクト: inanimateblob/Cardchan
        public async Task <List <Card> > GetCards(string name, int limit = 0)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException("name");
            }

            List <Card> cards = new List <Card>();

            name = this.mSearchUtility.GetRegexSearchValue(name);

            var collection = this.mDatabase.GetCollection <Card>(cCardsCollectionName);

            Stopwatch watch = new Stopwatch();

            watch.Start();

            var query = Query.And(
                Query <Card> .Matches(e => e.SearchName, new BsonRegularExpression(name, "i")),
                Query.NE("multiverseId", 0));

            MongoCursor <Card> cursor = collection.Find(query)
                                        .SetSortOrder("searchName");

            if (limit > 0)
            {
                cursor.SetLimit(limit);
            }

            foreach (Card card in cursor)
            {
                cards.Add(card);
            }

            watch.Stop();

            this.mLoggingService.Trace("Elapsed time: {0}", watch.Elapsed);

            return(cards);
        }
コード例 #10
0
        private MongoCursor <BsonDocument> Find(string collectionName, string query, string fields, string sort, int skip, int limit)
        {
            MongoCollection <BsonDocument> collection = Database.GetCollection <BsonDocument>(collectionName);
            MongoCursor <BsonDocument>     cursor     = (string.IsNullOrEmpty(query)) ? collection.FindAll() : collection.Find(new QueryDocument(ParseQuery(query)));

            if (!string.IsNullOrEmpty(fields))
            {
                ParseQuery(fields).ToList().ForEach(item =>
                {
                    if (item.Value == 0)
                    {
                        cursor.SetFields(Fields.Exclude(item.Name));
                    }
                    else
                    {
                        cursor.SetFields(Fields.Include(item.Name));
                    }
                });
            }

            if (!string.IsNullOrEmpty(sort))
            {
                ParseQuery(sort).ToList().ForEach(itemtoSort =>
                {
                    if (itemtoSort.Value > 0)
                    {
                        cursor.SetSortOrder(SortBy.Ascending(itemtoSort.Name));
                    }
                    else
                    {
                        cursor.SetSortOrder(SortBy.Descending(itemtoSort.Name));
                    }
                });
            }

            cursor.SetSkip(skip);
            cursor.SetLimit(limit);
            return(cursor);
        }
コード例 #11
0
        public DataResult <Task <DomainUserForAdmin> > GetAsyncSequence(DataQueryOptions filter)
        {
            var query = new List <IMongoQuery>(filter.Filters.Count);

            // filtering
            List <DataFilterRule> filters = filter.Filters.Where(f => f.Value != null).ToList();

            foreach (DataFilterRule dataFilterRule in filters)
            {
                DataFilterRule f = dataFilterRule;

                if (string.Compare(f.Name, NameOfHelper.PropertyName <DomainUserForAdmin>(x => x.UserName),
                                   StringComparison.OrdinalIgnoreCase) == 0 && f.Type == DataFilterTypes.Equal)
                {
                    // by user name
                    query.Add(Query.Text(f.Value.ToString().ToLowerInvariant()));
                }
                else if (string.Compare(f.Name, NameOfHelper.PropertyName <DomainUserForAdmin>(x => x.Email),
                                        StringComparison.OrdinalIgnoreCase) == 0 && f.Type == DataFilterTypes.Equal)
                {
                    // by email
                    string email      = f.Value.ToString().ToLowerInvariant();
                    var    expression = new BsonRegularExpression(string.Format("^{0}.*", Regex.Escape(email)));
                    query.Add(Query <UserEntity> .Matches(p => p.Email, expression));
                }
                else if (string.Compare(f.Name, NameOfHelper.PropertyName <DomainUserForAdmin>(x => x.ProductType),
                                        StringComparison.OrdinalIgnoreCase) == 0 && f.Type == DataFilterTypes.Equal)
                {
                    // by product type
                    var productId = Int32.Parse(f.Value.ToString());
                    query.Add(Query <UserEntity> .EQ(p => p.ProductId, productId));
                }
                else if (string.Compare(f.Name, NameOfHelper.PropertyName <DomainUserForAdmin>(x => x.Created),
                                        StringComparison.OrdinalIgnoreCase) == 0)
                {
                    // by created date
                    var date = (DateTime)f.Value;
                    switch (f.Type)
                    {
                    case DataFilterTypes.Equal:
                        query.Add(Query <UserEntity> .EQ(p => p.Created, date));
                        break;

                    case DataFilterTypes.LessThan:
                        query.Add(Query <UserEntity> .LT(p => p.Created, date));
                        break;

                    case DataFilterTypes.LessThanOrEqual:
                        query.Add(Query <UserEntity> .LTE(p => p.Created, date));
                        break;

                    case DataFilterTypes.GreaterThan:
                        query.Add(Query <UserEntity> .GT(p => p.Created, date));
                        break;

                    case DataFilterTypes.GreaterThanOrEqual:
                        query.Add(Query <UserEntity> .GTE(p => p.Created, date));
                        break;
                    }
                }
                else
                {
                    throw new NotSupportedException(string.Format("Filter {0} by property {1} is not supported", f.Type, f.Name));
                }
            }

            // Filter only users
            query.Add(Query <UserEntity> .EQ(p => p.Roles, DomainRoles.User));

            MongoCursor <UserEntity> cursor    = _userRepository.Collection.Find(query.Count > 0 ? Query.And(query) : Query.Null);
            IMongoSortBy             sortOrder = null;

            // sorting
            if (string.Compare(filter.OrderBy, NameOfHelper.PropertyName <DomainUserForAdmin>(x => x.UserName),
                               StringComparison.OrdinalIgnoreCase) == 0)
            {
                // order by name
                sortOrder = filter.OrderByDirection == OrderByDirections.Asc
                    ? SortBy <UserEntity> .Ascending(p => p.Name)
                    : SortBy <UserEntity> .Descending(p => p.Name);
            }
            else if (string.Compare(filter.OrderBy, NameOfHelper.PropertyName <DomainUserForAdmin>(x => x.Created),
                                    StringComparison.OrdinalIgnoreCase) == 0)
            {
                // order by created
                sortOrder = filter.OrderByDirection == OrderByDirections.Asc
                    ? SortBy <UserEntity> .Ascending(p => p.Created)
                    : SortBy <UserEntity> .Descending(p => p.Created);
            }
            else if (string.Compare(filter.OrderBy, NameOfHelper.PropertyName <DomainUserForAdmin>(x => x.ProductType),
                                    StringComparison.OrdinalIgnoreCase) == 0)
            {
                // order by product type
                sortOrder = filter.OrderByDirection == OrderByDirections.Asc
                    ? SortBy <UserEntity> .Ascending(p => p.ProductId)
                    : SortBy <UserEntity> .Descending(p => p.ProductId);
            }

            if (sortOrder != null)
            {
                cursor.SetSortOrder(sortOrder);
            }

            // paging

            if (filter.Skip.HasValue)
            {
                cursor.SetSkip(filter.Skip.Value);
            }

            if (filter.Take.HasValue)
            {
                cursor.SetLimit(filter.Take.Value);
            }

            // Count of results
            long?count = null;

            if (filter.Count)
            {
                count = cursor.Count();
            }

            // post-processing

            return(new DataResult <Task <DomainUserForAdmin> >(cursor.Select(GetUserDataAsync), count));
        }
コード例 #12
0
        public async Task <DataResult <Watch> > GetSequenceAsync(DataQueryOptions filter, string userId)
        {
            var query = new List <IMongoQuery>(filter.Filters.Count);

            // Filtering
            List <DataFilterRule> filters = filter.Filters.Where(f => f.Value != null).ToList();
            string requestedUserId        = null;
            var    searchList             = new List <string>();

            foreach (DataFilterRule dataFilterRule in filters)
            {
                DataFilterRule f = dataFilterRule;

                if (string.Compare(f.Name, NameOfHelper.PropertyName <Watch>(x => x.Name),
                                   StringComparison.OrdinalIgnoreCase) == 0 && f.Type == DataFilterTypes.Equal)
                {
                    // by name
                    searchList.Add(f.Value.ToString().ToLowerInvariant());
                }
                else if (string.Compare(f.Name, NameOfHelper.PropertyName <Watch>(x => x.UserId),
                                        StringComparison.OrdinalIgnoreCase) == 0 && f.Type == DataFilterTypes.Equal)
                {
                    // by user id
                    requestedUserId = f.Value.ToString();
                    query.Add(Query <ProjectEntity> .EQ(p => p.UserId, requestedUserId));
                }
                else if (string.Compare(f.Name, NameOfHelper.PropertyName <Watch>(x => x.Generator),
                                        StringComparison.OrdinalIgnoreCase) == 0 && f.Type == DataFilterTypes.Equal)
                {
                    // by product id
                    int productId = Int32.Parse(f.Value.ToString());
                    query.Add(Query <ProjectEntity> .EQ(p => p.ProductId, productId));
                }
                else if (string.Compare(f.Name, NameOfHelper.PropertyName <Watch>(x => x.ProjectType),
                                        StringComparison.OrdinalIgnoreCase) == 0 && f.Type == DataFilterTypes.Equal)
                {
                    // by project type
                    object projectType = Enum.Parse(typeof(ProjectType), f.Value.ToString());
                    query.Add(Query <ProjectEntity> .EQ(p => p.ProjectType, (int)projectType));
                }
                else if (string.Compare(f.Name, NameOfHelper.PropertyName <Watch>(x => x.ProjectSubtype),
                                        StringComparison.OrdinalIgnoreCase) == 0 && f.Type == DataFilterTypes.Equal)
                {
                    // by project subtype
                    object projectSubtype = Enum.Parse(typeof(ProjectSubtype), f.Value.ToString());
                    query.Add(Query <ProjectEntity> .EQ(p => p.ProjectSubtype, (int)projectSubtype));
                }
                else if (string.Compare(f.Name, NameOfHelper.PropertyName <Watch>(x => x.External.VideoUri),
                                        StringComparison.OrdinalIgnoreCase) == 0 && f.Type == DataFilterTypes.Equal)
                {
                    // by external video uri
                    query.Add(Query <ProjectEntity> .EQ(p => p.VideoSource, f.Value));
                }
                else if (string.Compare(f.Name, NameOfHelper.PropertyName <Watch>(x => x.Created),
                                        StringComparison.OrdinalIgnoreCase) == 0)
                {
                    // by created date
                    var date = (DateTime)f.Value;
                    switch (f.Type)
                    {
                    case DataFilterTypes.Equal:
                        query.Add(Query <ProjectEntity> .EQ(p => p.Created, date));
                        break;

                    case DataFilterTypes.LessThan:
                        query.Add(Query <ProjectEntity> .LT(p => p.Created, date));
                        break;

                    case DataFilterTypes.LessThanOrEqual:
                        query.Add(Query <ProjectEntity> .LTE(p => p.Created, date));
                        break;

                    case DataFilterTypes.GreaterThan:
                        query.Add(Query <ProjectEntity> .GT(p => p.Created, date));
                        break;

                    case DataFilterTypes.GreaterThanOrEqual:
                        query.Add(Query <ProjectEntity> .GTE(p => p.Created, date));
                        break;
                    }
                }
                else if (string.Compare(f.Name, NameOfHelper.PropertyName <Watch>(x => x.HitsCount),
                                        StringComparison.OrdinalIgnoreCase) == 0)
                {
                    // by hits count
                    int threshold = Int32.Parse(f.Value.ToString());

                    if (f.Type == DataFilterTypes.GreaterThan)
                    {
                        query.Add(Query <ProjectEntity> .GT(p => p.HitsCount, threshold));
                    }
                    else if (f.Type == DataFilterTypes.GreaterThanOrEqual)
                    {
                        query.Add(Query <ProjectEntity> .GTE(p => p.HitsCount, threshold));
                    }
                    else
                    {
                        query.Add(Query <ProjectEntity> .EQ(p => p.HitsCount, threshold));
                    }
                }
                else if (string.Compare(f.Name, NameOfHelper.PropertyName <Watch>(x => x.State),
                                        StringComparison.OrdinalIgnoreCase) == 0 && f.Type == DataFilterTypes.Equal)
                {
                    // by video state
                    var videoState = (WatchState)Enum.Parse(typeof(WatchState), f.Value.ToString());
                    if (videoState == WatchState.Uploading)
                    {
                        var avsx = Query <ProjectEntity> .EQ(p => p.AvsxFileId, null);

                        var originalFileId = Query <ProjectEntity> .EQ(p => p.OriginalVideoFileId, null);

                        var externalSource = Query <ProjectEntity> .EQ(p => p.VideoSource, null);

                        query.Add(Query.Or(avsx, Query.And(originalFileId, externalSource)));
                    }
                    else if (videoState == WatchState.Encoding)
                    {
                        var avsx = Query <ProjectEntity> .NE(p => p.AvsxFileId, null);

                        var originalFileId = Query <ProjectEntity> .NE(p => p.OriginalVideoFileId, null);

                        var encodedVideosName = NameOfHelper.PropertyName <ProjectEntity>(x => x.EncodedVideos);
                        var encodedVideos     = Query.NotExists(string.Format("{0}.0", encodedVideosName));

                        query.Add(Query.And(avsx, originalFileId, encodedVideos));
                    }
                    else if (videoState == WatchState.Ready)
                    {
                        var avsx = Query <ProjectEntity> .NE(p => p.AvsxFileId, null);

                        var externalSource = Query <ProjectEntity> .NE(p => p.VideoSource, null);

                        var originalFileId = Query <ProjectEntity> .NE(p => p.OriginalVideoFileId, null);

                        var encodedVideosName = NameOfHelper.PropertyName <ProjectEntity>(x => x.EncodedVideos);
                        var encodedVideos     = Query.Exists(string.Format("{0}.0", encodedVideosName));

                        query.Add(Query.And(avsx, Query.Or(externalSource, Query.And(originalFileId, encodedVideos))));
                    }
                }
                else
                {
                    throw new NotSupportedException(string.Format("Filter {0} by property {1} is not supported", f.Type, f.Name));
                }
            }

            string searchText = String.Join(" ", searchList);

            if (!String.IsNullOrEmpty(searchText))
            {
                query.Add(Query.Text(searchText));
            }

            // we should see only public videos for other users
            if (requestedUserId != userId)
            {
                query.Add(Query <ProjectEntity> .EQ(p => p.Access, (int)ProjectAccess.Public));
            }

            MongoCursor <ProjectEntity> cursor = _projectRepository.Collection.Find(query.Count > 0 ? Query.And(query) : Query.Null);

            // Sorting
            IMongoSortBy sortOrder = null;

            if (string.Compare(filter.OrderBy, NameOfHelper.PropertyName <Watch>(x => x.Name),
                               StringComparison.OrdinalIgnoreCase) == 0)
            {
                // order by name
                sortOrder = filter.OrderByDirection == OrderByDirections.Asc
                    ? SortBy <ProjectEntity> .Ascending(p => p.Name)
                    : SortBy <ProjectEntity> .Descending(p => p.Name);
            }
            else if (string.Compare(filter.OrderBy, NameOfHelper.PropertyName <Watch>(x => x.Created),
                                    StringComparison.OrdinalIgnoreCase) == 0)
            {
                // order by created
                sortOrder = filter.OrderByDirection == OrderByDirections.Asc
                    ? SortBy <ProjectEntity> .Ascending(p => p.Created)
                    : SortBy <ProjectEntity> .Descending(p => p.Created);
            }
            else if (string.Compare(filter.OrderBy, NameOfHelper.PropertyName <Watch>(x => x.HitsCount),
                                    StringComparison.OrdinalIgnoreCase) == 0)
            {
                // order by hits count
                sortOrder = filter.OrderByDirection == OrderByDirections.Asc
                    ? SortBy <ProjectEntity> .Ascending(p => p.HitsCount)
                    : SortBy <ProjectEntity> .Descending(p => p.HitsCount);
            }

            if (sortOrder != null)
            {
                cursor.SetSortOrder(sortOrder);
            }

            // Paging
            if (filter.Skip.HasValue)
            {
                cursor.SetSkip(filter.Skip.Value);
            }
            if (filter.Take.HasValue)
            {
                cursor.SetLimit(filter.Take.Value);
            }

            // Count of results
            long?count = null;

            if (filter.Count)
            {
                count = cursor.Count();
            }

            List <ProjectEntity> projects = cursor.ToList();

            // Get comments and total comment counts
            string[] projectIds = projects.Select(p => p.Id).ToArray();
            Dictionary <string, int> commentsCounts = (await _commentRepository.GetCommentsCountByProjectsAsync(projectIds)).ToDictionary(c => c.ProjectId, c => c.CommentsCount);
            Dictionary <string, List <CommentEntity> > recentComments = await _commentRepository.GetRecentCommentsByProjectsAsync(projectIds, RecentCommentsLimit);

            // Get project user ids united with comment user ids to load entities in batch
            string[] userIds = GetUserIds(projects, recentComments);
            Dictionary <string, UserEntity> users = (await _userRepository.GetUsersByIdsAsync(userIds)).ToDictionary(u => u.Id);

            // Post-processing
            return(new DataResult <Watch>(projects.Select(p => AggregateProject(p, userId, users, commentsCounts, recentComments)), count));
        }
コード例 #13
0
ファイル: RapideDdl_f_v1.cs プロジェクト: 24/source_04
        public static void Test_RapideDdl_Images_01()
        {
            //pb.Data.Mongo.MongoCommand.FindAs("dl", "RapideDdl_Detail", "{}", fields: "{ 'download.images.Source': 1, 'download.images.ImageWidth': 1, 'download.images.ImageHeight': 1 }", limit: 5);
            //string query = "{ 'download.images.ImageHeight': { $gt: 100 } }";
            string query  = "{}";
            string fields = "{ 'download.images.Source': 1, 'download.images.ImageWidth': 1, 'download.images.ImageHeight': 1 }";
            int    limit  = 0;
            MongoCursor <BsonDocument> cursor = MongoCommand.GetDatabase(null, "dl").GetCollection("RapideDdl_Detail").zFind <BsonDocument>(new QueryDocument(BsonSerializer.Deserialize <BsonDocument>(query)));

            cursor.SetFields(new FieldsWrapper(BsonSerializer.Deserialize <BsonDocument>(fields)));
            if (limit != 0)
            {
                cursor.SetLimit(limit);
            }
            //DataTable dt = new DataTable();
            //dt.Load()
            //dt.LoadDataRow()
            //dt.ReadXml()
            //foreach (BsonDocument document in cursor)
            //{
            //    BsonArray array = document.GetElement("download").Value.AsBsonDocument.GetElement("images").Value.AsBsonArray;
            //    foreach (BsonValue value in array)
            //    {
            //        //Trace.WriteLine("BsonType : {0}", value.BsonType);
            //        Trace.WriteLine(value.ToJson());
            //    }
            //}
            //DataTable dt = Test_Bson.Test_Bson_f.BsonDocumentsToDataTable(cursor);
            DataTable dt = cursor.zToDataTable2();

            //dt.Select();
            //dt.Rows[0].Delete
            //foreach (DataRow row in dt.Rows)
            //{
            //    object imageHeightValue = row["download.images.ImageHeight"];
            //    //if (imageHeight is int && (int)imageHeight <= 60)
            //    //    row.Delete();
            //    int imageHeight;
            //    if (imageHeightValue is string && int.TryParse((string)imageHeightValue, out imageHeight))
            //    {
            //        if (imageHeight <= 60)
            //            row.Delete();
            //    }
            //}

            for (int i = 0; i < dt.Rows.Count;)
            {
                DataRow row = dt.Rows[i];
                object  imageHeightValue = row["download.images.ImageHeight"];
                //if (imageHeight is int && (int)imageHeight <= 60)
                //    row.Delete();
                int imageHeight;
                if (imageHeightValue is string && int.TryParse((string)imageHeightValue, out imageHeight) && imageHeight <= 60)
                {
                    //if (imageHeight <= 60)
                    row.Delete();
                }
                else
                {
                    i++;
                }
            }

            RunSource.CurrentRunSource.SetResult(dt);
        }
コード例 #14
0
ファイル: MtgStore.cs プロジェクト: inanimateblob/Cardchan
        public async Task <List <Card> > AdvancedSearchCards(string name, int limit = 0)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException("name");
            }

            List <Card> cards = new List <Card>();

            string regex_name = this.mSearchUtility.GetRegexAdvancedSearchValue(name);

            var collection = this.mDatabase.GetCollection <Card>(cCardsCollectionName);

            Stopwatch watch = new Stopwatch();

            watch.Start();

            IMongoQuery        query  = null;
            MongoCursor <Card> cursor = null;

            string[] names = name.Split(' ');
            if (names.Length > 1)
            {
                List <IMongoQuery> nameQueries = new List <IMongoQuery>();

                foreach (string n in names)
                {
                    nameQueries.Add(Query.And(
                                        Query <Card> .Matches(e => e.SearchName, new BsonRegularExpression(this.mSearchUtility.GetRegexAdvancedSearchValue(n), "i")),
                                        Query.NE("multiverseId", 0)));
                }

                cursor = collection.Find(Query.And(nameQueries))
                         .SetSortOrder("searchName");
            }
            else
            {
                query = Query.And(
                    Query <Card> .Matches(e => e.SearchName, new BsonRegularExpression(regex_name, "i")),
                    Query.NE("multiverseId", 0));

                cursor = collection.Find(query)
                         .SetSortOrder("searchName");
            }

            if (limit > 0)
            {
                cursor.SetLimit(limit);
            }

            foreach (Card card in cursor)
            {
                cards.Add(card);
            }

            watch.Stop();

            this.mLoggingService.Trace("Elapsed time: {0}", watch.Elapsed);

            return(cards);
        }
コード例 #15
0
        public async Task <DataResult <Task <DomainProjectForAdmin> > > GetAsyncSequenceAsync(DataQueryOptions filter)
        {
            var query = new List <IMongoQuery>(filter.Filters.Count);

            // filtering
            List <DataFilterRule> filters = filter.Filters.Where(f => f.Value != null).ToList();

            foreach (DataFilterRule dataFilterRule in filters)
            {
                DataFilterRule f = dataFilterRule;

                if (string.Compare(f.Name, NameOfHelper.PropertyName <DomainProjectForAdmin>(x => x.Name),
                                   StringComparison.OrdinalIgnoreCase) == 0 && f.Type == DataFilterTypes.Equal)
                {
                    // by name
                    query.Add(Query.Text(f.Value.ToString().ToLowerInvariant()));
                }
                else if (string.Compare(f.Name, NameOfHelper.PropertyName <DomainProjectForAdmin>(x => x.ProductType),
                                        StringComparison.OrdinalIgnoreCase) == 0 && f.Type == DataFilterTypes.Equal)
                {
                    // by product type
                    var productId = Int32.Parse(f.Value.ToString());
                    query.Add(Query <ProjectEntity> .EQ(p => p.ProductId, productId));
                }
                else if (string.Compare(f.Name, NameOfHelper.PropertyName <DomainProjectForAdmin>(x => x.UserId),
                                        StringComparison.OrdinalIgnoreCase) == 0 && f.Type == DataFilterTypes.Equal)
                {
                    // by user id
                    query.Add(Query <ProjectEntity> .EQ(p => p.UserId, f.Value.ToString()));
                }
                else if (string.Compare(f.Name, NameOfHelper.PropertyName <DomainProjectForAdmin>(x => x.UserName),
                                        StringComparison.OrdinalIgnoreCase) == 0 && f.Type == DataFilterTypes.Equal)
                {
                    // by user name
                    List <UserEntity> profiles = await _userRepository.FindByNameAsync(f.Value.ToString());

                    if (profiles.Count == 0)
                    {
                        // no users found
                        return(new DataResult <Task <DomainProjectForAdmin> >(new Task <DomainProjectForAdmin>[] { }));
                    }

                    List <string> allIds = profiles.Select(prof => prof.Id).ToList();
                    query.Add(Query <ProjectEntity> .Where(p => allIds.Contains(p.UserId)));
                }
                else if (string.Compare(f.Name, NameOfHelper.PropertyName <DomainProjectForAdmin>(x => x.Created),
                                        StringComparison.OrdinalIgnoreCase) == 0)
                {
                    // by created date
                    var date = (DateTime)f.Value;
                    switch (f.Type)
                    {
                    case DataFilterTypes.Equal:
                        query.Add(Query <ProjectEntity> .EQ(p => p.Created, date));
                        break;

                    case DataFilterTypes.LessThan:
                        query.Add(Query <ProjectEntity> .LT(p => p.Created, date));
                        break;

                    case DataFilterTypes.LessThanOrEqual:
                        query.Add(Query <ProjectEntity> .LTE(p => p.Created, date));
                        break;

                    case DataFilterTypes.GreaterThan:
                        query.Add(Query <ProjectEntity> .GT(p => p.Created, date));
                        break;

                    case DataFilterTypes.GreaterThanOrEqual:
                        query.Add(Query <ProjectEntity> .GTE(p => p.Created, date));
                        break;
                    }
                }
                else
                {
                    throw new NotSupportedException(string.Format("Filter {0} by property {1} is not supported", f.Type, f.Name));
                }
            }

            if (!filters.Any() && !string.IsNullOrEmpty(filter.OrderBy))
            {
                // MongoDb 2.6 HACK!!!
                // adding fake query to hint proper index

                if (string.Compare(filter.OrderBy, NameOfHelper.PropertyName <DomainProjectForAdmin>(x => x.Name),
                                   StringComparison.OrdinalIgnoreCase) == 0)
                {
                    // order by name
                    query.Add(Query <ProjectEntity> .Exists(p => p.Name));
                }
                else if (string.Compare(filter.OrderBy, NameOfHelper.PropertyName <DomainProjectForAdmin>(x => x.Created),
                                        StringComparison.OrdinalIgnoreCase) == 0)
                {
                    // order by created
                    query.Add(Query <ProjectEntity> .Exists(p => p.Created));
                }
                else if (string.Compare(filter.OrderBy, NameOfHelper.PropertyName <DomainProjectForAdmin>(x => x.ProductType),
                                        StringComparison.OrdinalIgnoreCase) == 0)
                {
                    // order by product type
                    query.Add(Query <ProjectEntity> .Exists(p => p.ProductId));
                }
            }

            MongoCursor <ProjectEntity> cursor = _projectRepository.Collection.Find(query.Count > 0 ? Query.And(query) : Query.Null);
            IMongoSortBy sortOrder             = null;

            // sorting
            if (string.Compare(filter.OrderBy, NameOfHelper.PropertyName <DomainProjectForAdmin>(x => x.Name),
                               StringComparison.OrdinalIgnoreCase) == 0)
            {
                // order by name
                sortOrder = filter.OrderByDirection == OrderByDirections.Asc
                    ? SortBy <ProjectEntity> .Ascending(p => p.Name)
                    : SortBy <ProjectEntity> .Descending(p => p.Name);
            }
            else if (string.Compare(filter.OrderBy, NameOfHelper.PropertyName <DomainProjectForAdmin>(x => x.Created),
                                    StringComparison.OrdinalIgnoreCase) == 0)
            {
                // order by created
                sortOrder = filter.OrderByDirection == OrderByDirections.Asc
                    ? SortBy <ProjectEntity> .Ascending(p => p.Created)
                    : SortBy <ProjectEntity> .Descending(p => p.Created);
            }
            else if (string.Compare(filter.OrderBy, NameOfHelper.PropertyName <DomainProjectForAdmin>(x => x.ProductType),
                                    StringComparison.OrdinalIgnoreCase) == 0)
            {
                // order by product type
                sortOrder = filter.OrderByDirection == OrderByDirections.Asc
                    ? SortBy <ProjectEntity> .Ascending(p => p.ProductId)
                    : SortBy <ProjectEntity> .Descending(p => p.ProductId);
            }

            if (sortOrder != null)
            {
                cursor.SetSortOrder(sortOrder);
            }

            // paging

            if (filter.Skip.HasValue)
            {
                cursor.SetSkip(filter.Skip.Value);
            }

            if (filter.Take.HasValue)
            {
                cursor.SetLimit(filter.Take.Value);
            }

            // Count of results
            long?count = null;

            if (filter.Count)
            {
                count = cursor.Count();
            }

            // post-processing

            return(new DataResult <Task <DomainProjectForAdmin> >(cursor.Select(GetProjectDataAsync), count));
        }
コード例 #16
0
 public void UpdateCursor(MongoCursor cursor)
 {
     cursor.SetLimit(Count);
 }
コード例 #17
0
    public List <BsonDocument> ExecuteQueryBsonDoc(string table,                 // 表名
                                                   IMongoQuery query,            // 查询条件,外部要自拼接
                                                   string[] fields  = null,
                                                   string sort      = "",
                                                   bool asc         = true,
                                                   int skip         = 0,
                                                   int limt         = 0,
                                                   string[] indexes = null)
    {
        // 表不存在,直接返回
        if (!TableExists(table))
        {
            return(null);
        }

        List <BsonDocument> retlist = new List <BsonDocument>();

        try
        {
            var cb = check_table(table);
            MongoCursor <BsonDocument> ret = null;
            if (query == null)
            {
                ret = cb.FindAll();
            }
            else
            {
                ret = cb.Find(query);
            }

            if (fields != null)
            {
                ret = ret.SetFields(fields);
            }

            if (sort != string.Empty)
            {
                if (asc)
                {
                    ret = ret.SetSortOrder(SortBy.Ascending(sort));
                }
                else
                {
                    ret = ret.SetSortOrder(SortBy.Descending(sort));
                }
            }

            if (skip > 0)
            {
                ret = ret.SetSkip(skip);
            }
            if (limt > 0)
            {
                ret = ret.SetLimit(limt);
            }

            var it = ret.GetEnumerator();

            while (it.MoveNext())
            {
                retlist.Add(it.Current);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            retlist.Clear();
        }
        return(retlist);
    }
コード例 #18
0
 private void ApplyTake(MongoCursor<BsonDocument> cursor, int? takeCount)
 {
     if (takeCount.HasValue)
         cursor.SetLimit(takeCount.Value);
 }
コード例 #19
0
        /// <summary>
        /// Gets the cards.
        /// </summary>
        /// <returns>The cards.</returns>
        /// <param name="query">Query.</param>
        public async Task <Card[]> GetCards(dynamic query)
        {
            var client     = new MongoClient(Connection);
            var server     = client.GetServer();
            var database   = server.GetDatabase("mtg");
            var collection = database.GetCollection <Card> ("cards");

            MongoCursor <Card> cursor  = null;
            List <IMongoQuery> queries = new List <IMongoQuery> ();


            if (query.cardsetid != null)
            {
                queries.Add(Query <Card> .EQ(e => e.CardSetId,
                                             (string)query.cardsetid));
            }

            if (query.artist != null)
            {
                queries.Add(Query <Card> .EQ(e => e.Artist,
                                             (string)query.artist));
            }

            if (query.rarity != null)
            {
                queries.Add(Query <Card> .EQ(e => e.Rarity,
                                             (string)query.rarity));
            }

            if (query.loyalty != null)
            {
                queries.Add(Query <Card> .EQ(e => e.Loyalty,
                                             (int)query.loyalty));
            }

            if (query.toughness != null)
            {
                queries.Add(Query <Card> .EQ(e => e.Toughness,
                                             (int)query.toughness));
            }

            if (query.power != null)
            {
                queries.Add(Query <Card> .EQ(e => e.Power,
                                             (int)query.power));
            }

            if (query.subtype != null)
            {
                queries.Add(Query <Card> .EQ(e => e.SubType,
                                             (string)query.subtype));
            }


            if (query.cardsetname != null)
            {
                queries.Add(Query <Card> .EQ(e => e.CardSetName,
                                             (string)query.cardsetname));
            }

            if (query.convertedmanacost != null)
            {
                queries.Add(Query <Card> .EQ(e => e.ConvertedManaCost,
                                             (int)query.convertedmanacost));
            }

            if (query.setnumber != null)
            {
                queries.Add(Query <Card> .EQ(e => e.SetNumber,
                                             (int)query.setnumber));
            }

            if (query.manacost != null)
            {
                queries.Add(Query <Card> .EQ(e => e.ManaCost,
                                             (string)query.manacost));
            }

            if (query.colors != null)
            {
                foreach (string color in ((string)query.colors).ToString().Split(','))
                {
                    queries.Add(Query <Card> .EQ(e => e.Colors, color));
                }
            }

            if (query.name != null)
            {
                queries.Add(Query <Card> .EQ(e => e.SearchName,
                                             ((string)query.name).ToLower().Replace(" ", "")));
            }

            if (query.type != null)
            {
                queries.Add(Query <Card> .EQ(e => e.Type, (string)query.type));
            }

            if (query.id != null)
            {
                queries.Add(Query <Card> .EQ(e => e.Id, (int)query.id));
            }

            if (queries.Count > 0)
            {
                cursor = collection.Find(Query.And(queries)).SetSortOrder("_id");
            }
            else
            {
                cursor = collection.FindAllAs <Card> ().SetSortOrder("_id");
            }

            if (query.limit != null)
            {
                cursor.SetLimit((int)query.limit);
            }

            List <Card> cards = new List <Card> ();

            foreach (Card card in cursor)
            {
                cards.Add(card);
            }

            return(cards.ToArray());
        }
コード例 #20
0
 public IMongoCursor SetLimit(int limit)
 {
     return(new MongoCursorProxy(_mongoMongoCursor.SetLimit(limit)));
 }
コード例 #21
0
        private static void Execute(FlexibleOptions options)
        {
            Logger.Info("Start");

            // Args Sanity Check
            if (options.Options == null || options.Options.Count == 0)
            {
                Console.WriteLine("No arguments received.");
                System.Environment.Exit(-101);
            }

            // Prompts for user Input
            Console.WriteLine("Is the configuration correct ? Y/N");
            var key = Console.ReadKey().Key;

            // Checking Key
            if (key == ConsoleKey.N) // N = "NO"
            {
                Console.WriteLine(" => 'NO' : Aborting");
                System.Environment.Exit(-102);
            }
            else if (key != ConsoleKey.Y) // Anything other than "N" and "Y" is an error.
            {
                Console.WriteLine(" => 'Wrong Key Pressed' : Expected either 'Y' or 'N'");
                System.Environment.Exit(-102);
            }

            Console.WriteLine(" => Proceeding with Export.");

            // Sanity Check of Config and Arguments
            if (!ValidateConfig(options))
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Missing MongoDB Configuration Parameter. (Server, Database, Collection and Credentials are Mandatory)");
                Console.ForegroundColor = ConsoleColor.White;
                System.Environment.Exit(-103);
            }

            // Creating instance of MongoDB
            String sourceConnString = MongoDbContext.BuildConnectionString(options["sourceUsername"], options["sourcePassword"], options["sourceServer"], options["authDatabaseName"]);

            // Reaching Databases
            MongoDatabase sourceDatabase = MongoDbContext.GetServer(sourceConnString).GetDatabase(options["sourceDatabaseName"]);

            // Assembling "Query" to MongoDB, if any query text was provided
            QueryDocument query = String.IsNullOrWhiteSpace(options["mongoQuery"]) ? null : new QueryDocument(QueryDocument.Parse(options["mongoQuery"]));

            // Checking if the provided Collection Exists
            if (!sourceDatabase.CollectionExists(options["collection"]))
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Collection [ " + options["collection"] + " ] does not exists on the specified database");
                Console.ForegroundColor = ConsoleColor.White;
                System.Environment.Exit(-104);
            }

            if (options["format"].ToUpper() == "CSV")
            {
                // Loading Export Configuration from XML File
                if (!JsonToCSV.LoadExportLayout(options["layoutFile"]))
                {
                    // Error Checking
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Error Loading Export Layout");
                    Console.WriteLine("Message : " + JsonToCSV.errorMessage);
                    Console.ForegroundColor = ConsoleColor.White;
                    System.Environment.Exit(-105);
                }
            }

            // Setting up MongoDB Cursor
            MongoCursor cursor = sourceDatabase.GetCollection <BsonDocument> (options["collection"]).Find(query);

            cursor.SetFlags(QueryFlags.NoCursorTimeout);

            // Checking for the need to apply limit
            int _limit = options.Get <int>("limit", -1);

            if (_limit != -1)
            {
                cursor.SetLimit(_limit);
            }

            // Counters
            int recordsProcessed = 0;

            // JSON Settings to keep the "JSON" output as "Strict"
            var jsonSettings = new JsonWriterSettings()
            {
                OutputMode = JsonOutputMode.Strict
            };

            // File Writer
            using (StreamWriter fWriter = new StreamWriter(options["outputFile"], false, Encoding.UTF8))
            {
                // Auto Flush
                fWriter.AutoFlush = true;

                // Output File Line
                string fileLine = String.Empty;

                // Should we add headers to the output CSV file?
                if (options["format"].ToUpper() == "CSV" && options.Get <bool>("addHeader", false))
                {
                    // Writing Headers
                    fWriter.WriteLine(JsonToCSV.Fields);
                }

                // Iterating over documents found using the query
                foreach (BsonDocument document in cursor)
                {
                    // Picking which export method will be used
                    if (options["format"].ToUpper() == "CSV")
                    {
                        // Extracting data from it
                        fileLine = JsonToCSV.BsonToCSV(document);
                    }
                    else
                    {
                        fileLine = document.ToJson(jsonSettings);
                    }

                    // Checking for errors
                    if (String.IsNullOrWhiteSpace(fileLine))
                    {
                        continue;
                    }

                    // Writing to output csv
                    fWriter.WriteLine(fileLine.Replace(System.Environment.NewLine, "<br>"));

                    // Counting
                    if (recordsProcessed++ % 100 == 0)
                    {
                        Console.WriteLine("Processed : " + recordsProcessed);
                    }
                }
            }

            Logger.Info("End");
        }