2
我目前正在玩弄MongoDB使用它的C#驱动程序,尝试API(这将联盟更好,如果他们提供了一些例子)每个方法一个接一个。MongoDB C#:Update.pullAll不删除项目
而目前我在Update.PullAll()
方法。
我使用这个POCO对象为我的测试:
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对象:
{
"_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"
}
]
}
现在,我试图从技能集合中删除元素。
这里是我的代码:
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对象保持不变。
任何人都可以帮我指出我的代码在哪里出错了?
任何建议非常感谢,谢谢。
当我看到PullAll()时,我认为它的功能与InsertBatch()相同,所以这就是我出错的地方。谢谢!这帮助了很多。 – Drew 2012-04-25 07:51:28