Example #1
0
        private void SaveMetric(MetricBase metric, TObjectState state, BsonValue id, string collectionName, Dictionary<string, BsonValue> values)
        {
            IMongoDatabase database = GetDatabase(DATABASE_NAME, true);
            IMongoCollection<BsonDocument> collection = database.GetCollection<BsonDocument>(collectionName);

            if (state == TObjectState.Add)
            {
                BsonDocument newItem = new BsonDocument();
                BsonHelper.SetValue(newItem, "Name", metric.Name);
                BsonHelper.SetValue(newItem, "Value", metric.Value);
                foreach (KeyValuePair<string, BsonValue> pair in values)
                {
                    newItem[pair.Key] = pair.Value;
                }

                FilterDefinition<BsonDocument> filter = Builders<BsonDocument>.Filter.Eq("_id", id);
                UpdateDefinition<BsonDocument> update = Builders<BsonDocument>.Update.AddToSet("Metrics", newItem);
                UpdateOptions options = new UpdateOptions();
                options.IsUpsert = true;
                collection.UpdateOne(filter, update, options);
            }
            else if (state == TObjectState.Update)
            {
                FilterDefinition<BsonDocument> clientOrOrganisationIDFilter = Builders<BsonDocument>.Filter.Eq("_id", id);
                FilterDefinition<BsonDocument> metricNameFilter = Builders<BsonDocument>.Filter.Eq("Metrics.Name", metric.Name);
                FilterDefinition<BsonDocument> filter = Builders<BsonDocument>.Filter.And(clientOrOrganisationIDFilter, metricNameFilter);
                UpdateDefinition<BsonDocument> update = Builders<BsonDocument>.Update.Set("Metrics.$.Value", metric.Value);
                collection.UpdateOne(filter, update);
            }
            else if (state == TObjectState.Delete)
            {
                FilterDefinition<BsonDocument> filter = Builders<BsonDocument>.Filter.Eq("_id", id);
                UpdateDefinition<BsonDocument> update = Builders<BsonDocument>.Update.PullFilter("Metrics", Builders<BsonDocument>.Filter.Eq("Name", metric.Name));
                collection.UpdateOne(filter, update);
            }
        }
Example #2
0
 private void LoadMetricBase(MetricBase result, BsonDocument item)
 {
     result.Name = BsonHelper.GetString(item, "Name");
     result.Value = BsonHelper.GetLong(item, "Value").Value;
 }