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