public DataModel Update(DataModel model) { var resultModel = new DataModel() { operationSuccesfull = false }; var originalDoc = GetBsonDoc(model); if (originalDoc != null) { var replacementDoc = originalDoc.Merge(model.getBsonModel(), true); var replacementResult = getMongoCollection(model).ReplaceOne(model.getOrFilter(), replacementDoc); if (replacementResult.ModifiedCount == 1) { resultModel.operationSuccesfull = true; replacementDoc.Remove("_id"); resultModel.jsonModel = replacementDoc.ToJson(new JsonWriterSettings { OutputMode = JsonOutputMode.Strict }); } } return resultModel; }
public IList<DataModel> GetMany(DataModel model) //TO DO { var records = getMongoCollection(model).Find(model.getOrFilter()).ToList(); List<DataModel> result = new List<DataModel>(); foreach (var record in records) { record.Remove("_id"); result.Add(new DataModel() { jsonModel = record.ToJson(new JsonWriterSettings { OutputMode = JsonOutputMode.Strict }), operationSuccesfull = true }); } return result.AsReadOnly(); }
public DataModel Delete(DataModel model) { var result = getMongoCollection(model).DeleteOne(model.getOrFilter()); return new DataModel() { operationSuccesfull = (result.DeletedCount == 1) }; }
/// <summary> /// MongoDb -> Find with Filter defined in DataModel and return BsonDocument /// </summary> private BsonDocument GetBsonDoc(DataModel model) => getMongoCollection(model).Find(model.getOrFilter()).ToList().FirstOrDefault();