0

I have added a user to my mongodb collection called users using this model:

    [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; }

as you can se it also have a list of teams in it. So now im using this code to get the user:

         var query_id = Query.EQ("_id", ObjectId.Parse(Session["ID"].ToString()));
        User entity = Context.Users.FindOne(query_id);

And now i want to add a list to the mongodb object with the data from my team model:

  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; }

How do i add the team-modeldata to my userobject in the mongodb collection?

1 Answer 1

1

I suggest using LINQ / AutoMapping features of MongoDB.

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" });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.