2

I am new to learning how to use MongoDB and am stuck pretty early on, so hoping someone can help me out with a simple example.

I can successfully connect to a Mongo server and create a collection and create objects to put in it. I am doing all of this through c# and the c# driver.

I have the following custom objects defined in my code.

public class Feature
{
    public string FeatureName { get; set; }
    public ObjectId Id { get; set; }
    public List<Scenario> Scenarios { get; set; } 
}

public class Scenario
{
    private string _notes;
    public string ScenarioName { get; set; }
    public List<string> Givens { get; set; }
    public List<string> Whens { get; set; }
    public List<string> Thens { get; set; }
    public string Explanation { get; set; }
    public string Notes { get; set; }
}

As you can see, the Feature object contains a property which is a list of scenarios.

In my Mongo collection I have a Feature object that contains 3 scenarios. What I want to do in C# is write a method that can remove a specific scenario from a feature:

  1. User provides a feature and scenario name to a method
  2. Method checks the feature, looking through the list within it, for a scenario where the scenario.scenarioname matches the scenario name passed in to the method
  3. If it exists, then remove the scenario from the feature and update Mongo and return a bool of true. If it doesn't exist return false

I am sure that this is probably going to be obvious when I see an example, but I have tried and get stuck trying to work through the List property looking for a property on a sub object.

I hope that all makes sense??!?

Thanks in advance.

P

2
  • 1
    IF you have answered your question you should submit it as an answer to this question. If you then have follow-on questions, you should ask them as separate questions and link back to this for background, if necessary. You should not edit your question with a different question. Commented Jun 12, 2014 at 13:17
  • 1
    I reverted your edit. Please post the answer you found as an answer to this question. Answering your own question isn't frowned upon on stackexchange - it is even encouraged (you get a badge for it). When someone has a better answer, they will likely post it. When your answer can be improved, people will likely comment on it and suggest improvements. Commented Jun 12, 2014 at 14:32

1 Answer 1

2

Resolved it myself...

public bool DeleteScenario(string featureName, string scenarioName)
    {
        var collection = GetCollection<Feature>();
        var query = Query.EQ("FeatureName", featureName);
        var resultingFeature = collection.Find(query).SetLimit(1).FirstOrDefault();

        if (resultingFeature == null)
        {
            return false;
        }

        // we have found our feature and it exists.

        foreach (var scenario in resultingFeature.Scenarios)
        {
            if (scenario.ScenarioName == scenarioName)
            {
                resultingFeature.Scenarios.Remove(scenario);
                collection.Save(resultingFeature);
                return true;
            }
        }

        return true;

    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.