Exemple #1
0
        public static IEnumerable <MongoCursor <TType> > Split <TType>(this MongoDbCollection collection,
                                                                       IMongoQuery query,
                                                                       int splits, Action <MongoCursor <TType> > initializer = null, double sampleStart = 0,
                                                                       double sampleEnd = 1)
        {
            var sampler = new MongoIdSampler();
            var width   = (sampleEnd - sampleStart) / splits;

            for (var i = 0; i < splits; i++)
            {
                var q     = query ?? new QueryDocument();
                var start = sampleStart + i * width;
                var end   = sampleStart + (i + 1) * width;
                if (start > 0 || end < 1)
                {
                    q = Query.And(sampler.GetIdRange(start, end), q);
                }

                var cursor = collection.FindAs <TType>(q);
                if (initializer != null)
                {
                    initializer(cursor);
                }

                yield return(cursor);
            }
        }
 public async Task AddItemAsync <T>(MongoDbCollection collectionName, T item) where T : DomainBase
 {
     if (string.IsNullOrWhiteSpace(item.Id))
     {
         item.Id = Guid.NewGuid().ToString();
     }
     var collection = database.GetCollection <T>(collectionName.ToString());
     await collection.InsertOneAsync(item);
 }
        public async Task UpdateItemAsync(MongoDbCollection collectionName, BsonDocument filter, BsonDocument updatedFields, bool isManyUpdate = false)
        {
            var collection = database.GetCollection <BsonDocument>(collectionName.ToString());

            if (isManyUpdate)
            {
                await collection.UpdateManyAsync(filter, updatedFields);
            }
            else
            {
                await collection.UpdateOneAsync(filter, updatedFields);
            }
        }
        public async Task DeleteAsync(MongoDbCollection collectionName, BsonDocument filter, bool isManyDelete = false)
        {
            var collection = database.GetCollection <BsonDocument>(collectionName.ToString());

            if (isManyDelete)
            {
                await collection.DeleteManyAsync(filter);
            }
            else
            {
                await collection.DeleteOneAsync(filter);
            }
        }
        public async Task <IEnumerable <Hashtable> > GetItemsAsync(MongoDbCollection collectionName, BsonDocument filter, string[] fields = null)
        {
            var collection = database.GetCollection <BsonDocument>(collectionName.ToString());
            var cursor     = collection.Find(filter);

            List <BsonDocument> result;

            if (fields != null && fields.Any())
            {
                var projections         = fields.Select(field => Builders <BsonDocument> .Projection.Include(field)).ToList();
                var combinedProjections = Builders <BsonDocument> .Projection.Combine(projections);

                result = await cursor.Project(combinedProjections).ToListAsync();
            }
            else
            {
                result = await cursor.ToListAsync();
            }
            return(result.Select(e => e.ToHashtable()));
        }
 public MongoDbDataSource(MongoDbCollection collection, int threads = 2)
 {
     _collection            = collection;
     DeserializationThreads = threads;
 }