从MongoDB中数组删除元素 [英] Removing an element from an array in MongoDB

查看:239
本文介绍了从MongoDB中数组删除元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来学习如何使用MongoDB的和我坚持pretty的早,所以希望有人能帮助我用一个简单的例子。

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.

我可以成功连接到一个蒙戈服务器并创建一个收集和创建对象把它。我做的这一切,通过C#和C#的驱动程序。

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.

我在code定义以下的自定义对象。

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.

在我蒙戈集合我有一个包含3方案Feature对象。我想在C#中写的是可以从功能删除特定场景的方法:

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. 用户提供了一个功能和场景名称的方法<​​/ li>
  2. 方法检查功能,翻翻其中的列表,对于这样一个场景,在scenario.scenarioname传递的方案名称相匹配的方法
  3. 如果它存在,那么从功能删除场景和更新蒙戈,并返回一个真布尔。如果它不存在返回false
  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??!?

在此先感谢。

P

推荐答案

解决它自己...

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;

    }

这篇关于从MongoDB中数组删除元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆