我已经使用此模型将一个用户添加到名为users的mongodb集合中:
[BsonRepresentation(BsonType.ObjectId)]
public ObjectId _id { get; set; }
[Display(Name = "Password:")]
public string Password { get; set; }
[Display(Name = "Confirm:")]
public string ConfirmPassword { get; set; }
[Display(Name = "Email:")]
public string Email { get; set; }
[Display(Name = "Username:")]
public string UserName { get; set; }
[Display(Name = "Firtname:")]
public string Firstname { get; set; }
[Display(Name = "Lastname:")]
public string Lastname { get; set; }
[Display(Name = "Country:")]
public string Country { get; set; }
[Display(Name = "City:")]
public string City { get; set; }
[Display(Name = "Birthdate:")]
public int Birthdate { get; set; }
public List<Team> Teams { get; set; }
你可以看到它里面还有一个团队列表。所以现在我使用这段代码来获取用户:
var query_id = Query.EQ("_id", ObjectId.Parse(Session["ID"].ToString()));
User entity = Context.Users.FindOne(query_id);
现在我想用我的团队模型中的数据向mongodb对象添加一个列表:
public int TeamID { get; set; }
public string TeamName { get; set; }
public string UserName { get; set; }
public int LeagueID { get; set; }
public Points Points = new Points();
public List<Player> Player { get; set; }
如何在mongodb集合中将team-modeldata添加到我的userobject?
答案 0 :(得分:1)
我建议使用MongoDB的LINQ / AutoMapping功能。
var databaseClient = new MongoClient(DatabaseConnectionString);
var server = databaseClient.GetServer();
var database = server.GetDatabase("Users");
var collection = database.GetCollection<User>("users");
var user = collection.AsQueryable().First(o => o._id == YOURSESSIONID);
user.Teams.Add(new Team { TeamID = 0, TeamName = "Some Team" });