public void Upload(string job_id, UploadedFile file) { var gfs = Repository.Database.GridFS; var options = new MongoGridFSCreateOptions { Metadata = new BsonDocument(new BsonElement("job_id", job_id)) }; var info = gfs.Upload(file.InputStream, file.FileName, options); var attachment = FromFileInfo(info); var query = Query.EQ("_id", ObjectId.Parse(job_id)); var update = UpdateBuilder.AddToSet("attachment_ids", attachment.id); Jobs.Collection.Update(query, update); var units = Plugin.TryParse(attachment); if (units.Count() > 0) { var ids = new List <string>(); foreach (var unit in units) { ids.Add(Units.Create(unit).id); } var units_update = UpdateBuilder.AddToSetEach("unit_ids", new BsonArray(ids)); Jobs.Collection.Update(query, units_update); } }
public virtual void SaveVersioned(TEntity entity) { ++entity.Version; try { var query = Query.And( Query.EQ("_id", BsonValue.Create(entity.Id)), Query.EQ("Version", BsonValue.Create(entity.Version - 1))); var update = MDBUpdate.Replace(entity); Collection.Update(query, update); //LastErrorResponse response = MDb.LastError(); //// If it fails then "updateExisting" won't be available //if (response.Code == (int)ErrorCodes.DuplicateKeyOnUpdate) // throw new DuplicateKeyException(CollectionName, response.Error); //if (!(bool)response["updatedExisting"]) //{ // TEntity existingEntity = Collection.FindOne(new { _id = entity.Id }); // if (existingEntity == null) // throw new MissingResourceException(string.Format("Could not update non-existing {0} with Id {1}.", typeof(TEntity), entity.Id)); // else // throw new VersionConflictException(entity.Id.ToString(), typeof(TEntity), entity.Version); //} } catch (Exception) { --entity.Version; throw; } }
public override bool Delete(string id) { var query = Query.Exists("attachment_ids"); var update = UpdateBuilder.Pull("attachment_ids", id); Jobs.Collection.Update(query, update); Repository.Database.GridFS.DeleteById(ObjectId.Parse(id)); return(true); }
// Add new or update existing public virtual void Put(TEntity entity) { Condition.Requires(entity, "entity").IsNotNull(); var query = Query.EQ("_id", BsonValue.Create(entity.Id)); var update = MDBUpdate.Replace(entity); Collection.Update(query, update); //(new { _id = entity.Id }, entity, false, true); //LastErrorResponse response = MDb.LastError(); //if (response.Code == (int)ErrorCodes.DuplicateKeyOnAdd || response.Code == (int)ErrorCodes.DuplicateKeyOnUpdate) // throw new DuplicateKeyException(CollectionName, response.Error); }
public void SaveOrUpdate(TEntity entity) { var idValue = GetEntityId(entity); if (idValue != 0) { var update = MongoDBUpdate.Replace(entity); var result = Collection.Update( Query.EQ("_id", idValue), update, UpdateFlags.Upsert, WriteConcern.Acknowledged); } else { Save(entity); } }
public long Next() { var query = Query.EQ("_id", BsonValue.Create(CounterName)); var sort = SortBy.Ascending("_id"); var update = MDBUpdate.Inc("Count", 1); FindAndModifyResult result = Collection.FindAndModify(query, sort, update, true); if (result.ModifiedDocument == null) { AddInitialEntry(); result = Collection.FindAndModify(query, sort, update, true); } if (result.ModifiedDocument == null) { throw new InvalidOperationException(string.Format("Failed to update counter '{0}'.", CounterName)); } return(result.ModifiedDocument.GetValue("Count").ToInt64()); }
public virtual bool Update(string id, TEntity entity) { var query = Query.EQ("_id", ObjectId.Parse(id)); var updates = new List <IMongoUpdate>(); var document = entity.ToBsonDocument(); var properties = (entity.properties ?? document.Elements.Select(e => e.Name)).Except(Ignore); foreach (var property in properties) { updates.Add(UpdateBuilder.Set(property, document[property])); } if (updates.Count > 0) { updates.Add(UpdateBuilder.Set("updated_at", DateTime.UtcNow)); } var result = Collection.Update(query, UpdateBuilder.Combine(updates)); return(result.UpdatedExisting); }