MongoDB C#:Update.pullAll 不删除项目 [英] MongoDB C#: Update.pullAll not removing items

查看:23
本文介绍了MongoDB C#:Update.pullAll 不删除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用其 C# 驱动程序玩 MongoDB,尝试 API (如果他们提供一些示例会更好)一一.

I'am currently playing around with MongoDB using its C# driver, trying each method in the API (which would be leagues better if they provided some examples) one by one.

目前我正在使用 Update.PullAll() 方法.

And currently I'm on the Update.PullAll() method.

我在测试中使用这个 POCO 对象:

I'm using this POCO object for my tests:

public class Person 
{
    public ObjectId Id { get; set; }
    public string Firstname { get; set; }
    public string Lastname { get; set; }
    public List<Skill> Skills { get; set; }
}

public class Skill 
{
    public Object Id { get; set; }
    public string Name { get; set; }
}


如果填充,则转换为这个 json 对象:

If populated, translates into this json object:

{
   "_id": ObjectId("4f979621682dbc1a8cefecb3"),
   "Firstname" : "John",
   "Lastname" : "Doe",
   "Skills" : [
       {
          "_id" : ObjectId("4f979621682dbc1a8cefecaf"),
          "Name" : "C#.NET"
       },
       {
          "_id" : ObjectId("4f979621682dbc1a8cefecb0"),
          "Name" : "ASP.NET"
       },
       {
          "_id" : ObjectId("4f979621682dbc1a8cefecb1"),
          "Name" : "SQL Server"
       }
   ]
}

现在,我正在尝试从技能集合中删除元素.

Now, I'm trying to remove the elements from the skills collection.

这是我的代码:

var server = MongoServer.Create("mongodb://localhost/?safe=true");
var db =  server.GetDatabase("test");
var collection = db.GetCollection<Person>("person");

// Retrieve the data above from  mongoDB
var _person = collection.AsQueryable()
                        .Select(p => p).Single();

// Query to reference "John Doe"
var query = Query.EQ("_id",new ObjectId(_person.Id.ToString()));

// Collection of "John Doe's" skill ids
var objIds = _person.Skills
                    .Select(p => new ObjectId(p.Id.ToString()));

// Parse the skill ids to a BsonArray
var bsonArray = BsonArray.Create(objIds);

var update = Update.PullAll("Skills" , bsonArray);

// Call the Update command
collection.Update( query, update );

哪个,什么都不做;它使我的 json 对象完好无损.

Which, does nothing; it leaves my json object intact.

谁能帮我指出我的代码哪里出错了?
非常感谢任何建议,谢谢.

Can anyone help me point out where did I go wrong in my code?
Any suggestion is much appreciated, thanks.

推荐答案

要使 $pullAll 工作,您必须精确匹配整个对象,并且不能只使用一个字段(即使它是称为_id).

For $pullAll to work, you have to match the whole object exactly, and cannot use just one field (even if it is called _id).

因此,您还必须在更新命令中包含名称(字段也需要按相同顺序排列).

So you'd have to include the name as well in the update command (and the fields need to be in the same order, too).

你真正想要使用的命令是$pull,它满足你的需求,即匹配过滤条件:

The command you really want to use is $pull, which does what you need here, i.e. matching filtering criteria:

除了匹配精确值,您还可以使用表达式 ($pull这种方式很特别).

In addition to matching an exact value you can also use expressions ($pull is special in this way).

即使它被称为不带所有的拉",它确实拉所有匹配的元素,而不仅仅是一个.全部"仅表示您有一个过滤器(而 pullAll 有多个要匹配的元素).

Even though it is called "pull without all", it does pull all matched elements, not just one. The "all" just means that you have one filter (whereas pullAll has multiple elements to match).

这篇关于MongoDB C#:Update.pullAll 不删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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