Esempio n. 1
0
        public void Update(string id, Stream stream, string name = "")
        {
            var result = fileCollection.Find(new FilterDefinitionBuilder <MongoFile>().Eq(f => f._id, new ObjectId(id)));

            var path   = result.First().Path;
            var update = result.First();

            if (name != "")
            {
                update.Name      = name;
                update.Extension = Path.GetExtension(name);
                update.Path      = Path.ChangeExtension(update.Path, update.Extension);
                var filter = new FilterDefinitionBuilder <MongoFile>().Eq(f => f._id, update._id);
                var up     = new ObjectUpdateDefinition <MongoFile>(update);
                var up2    = new UpdateDefinitionBuilder <MongoFile>().Set(f => f.Name, update.Name)
                             .Set(f => f.Extension, update.Extension)
                             .Set(f => f.Path, update.Path);
                fileCollection.UpdateOne(filter, up2);
            }
            var fs = new FileStream(path, FileMode.Create);

            byte[] bytes = new byte[1024];
            int    num   = 0;

            do
            {
                num = stream.Read(bytes, 0, 1024);
                Console.WriteLine(bytes);
                fs.Write(bytes, 0, num);
            } while (num > 0);
            fs.Close();
            stream.Close();
            File.Move(path, update.Path);
        }
        public long UpdateManyField(Expression <Func <T, bool> > expression,
                                    IEnumerable <KeyValuePair <Expression <Func <T, object> >, object> > updates)
        {
            var filter = Builders <T> .Filter.Where(expression);

            UpdateDefinition <T> update = new ObjectUpdateDefinition <T>(expression);

            foreach (var(key, value) in updates)
            {
                update = update.Set(key, value);
            }
            update = update.Set(x => x.UpdatedTime, DateTime.Now);
            var result = _collection.UpdateOne(filter, update);

            return(result.ModifiedCount);
        }
Esempio n. 3
0
        public async void Update(T instance)
        {
            try
            {
                Expression <Func <T, bool> > filter = x => x.Id == instance.Id;

                var update = new ObjectUpdateDefinition <T>(instance);

                await Collection.UpdateOneAsync <T>(filter, update);
            }
            catch (Exception ex)
            {
                Context.Logger.LogError(ex, "Update");

                throw ex;
            }
        }
Esempio n. 4
0
        private async void Watcher_Created(object sender, FileSystemEventArgs e)
        {
            if (File.GetAttributes(e.FullPath) == FileAttributes.Directory)
            {
            }
            else
            {
                FileInfo info  = new FileInfo(e.FullPath);
                var      array = e.Name.Split('_');
                if (fileCollection.Find <MongoFile>(new FilterDefinitionBuilder <MongoFile>().Eq(f => f._id, new ObjectId(array[0]))).CountDocuments() == 0)
                {
                    var id = new ObjectId(array[0]);

                    var file = new MongoFile()
                    {
                        _id        = id,
                        PublicId   = Guid.NewGuid(),
                        SystemName = e.Name,
                        CreatedAt  = File.GetCreationTimeUtc(e.FullPath),
                        EditedAt   = File.GetLastWriteTimeUtc(e.FullPath),
                        Path       = e.FullPath,
                        Attributes = info.Attributes.ToString(),
                        Length     = info.Length,
                        Extension  = info.Extension
                    };
                    var update = new ObjectUpdateDefinition <MongoFile>(file);

                    var model = new List <WriteModel <MongoFile> >()
                    {
                        new UpdateOneModel <MongoFile>(
                            new FilterDefinitionBuilder <MongoFile>().Eq(f => f._id, id),
                            update
                            )
                        {
                            IsUpsert = true
                        }
                    };
                    //fileCollection.BulkWrite(model);
                    await fileCollection.InsertOneAsync(file);

                    //  fileCollection.(new FilterDefinitionBuilder<MongoFile>().Eq(f=>f._id,id),file);
                }
            }
            Console.WriteLine($"CREATE {e.Name}");
        }
Esempio n. 5
0
        public async Task <long> Update(T instance)
        {
            try
            {
                Expression <Func <T, bool> > filter = x => x.Id == instance.Id;

                var update = new ObjectUpdateDefinition <T>(instance);

                var result = await Collection.UpdateOneAsync <T>(filter, update);

                return(result.ModifiedCount);
            }
            catch (Exception ex)
            {
                Context.Logger.LogError(ex, "Update");

                throw;
            }
        }
        public JObject Edit(string collection, string id, JObject data)
        {
            JObject beforeUpdate = Get(collection, id);
            JObject clone        = (JObject)beforeUpdate.DeepClone();

            foreach (var(key, value) in data)
            {
                if (data[key] != null)
                {
                    clone[key] = data[key];
                }
            }

            BsonDocument doc = clone.ToBsonDocument();
            UpdateDefinition <BsonDocument> updateDef = new ObjectUpdateDefinition <BsonDocument>(doc);
            BsonDocument update = GetCollection(collection)
                                  .FindOneAndUpdate(obj => obj[idField] == BsonValue.Create(id), updateDef);

            return(update.ToJObject());
        }