我有以下用户对象。如何通过mongodb更新构建器为用户添加新的喜欢或不喜欢的朋友。我开始但我无法让它工作。示例代码如下。谢谢
public class User : Entity
{
public string Name { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public Dictionary<string, Dictionary<string, Friend>> Friends{ get; set; }
}
public class Circle
{
public string Name { get; set; }
}
以下是mongo json的样子
{
"Email" : null,
"Name" : "Nick",
"Password" : null,
"Friends" : {
"Liked" : {
"WER2343" : {
"Name" : "Friend 1"
}
},
"Disliked" : {
"M3222343" : {
"Name" : "Friend 2"
}
}
},
"_id" : ObjectId("513b5d84bfeddf1c8091530e")
}
这是我正在努力工作的代码
public void AddFriend(User self, User friend, string circle)
{
IMongoQuery query = Query
.And(Query<User>.EQ(t => t.Id, self.Id));
IMongoUpdate update = Update<User>
.Push(t => t.Friends)
Collection.FindAndModify(query,null, update);
}