private void btnSave_Click(object sender, EventArgs e) { if (rbEntity1.Checked) { var _users = _database.GetCollection <Users3>("users"); var user = new Users3 { }; user.Age = Convert.ToInt32(txtAge.Text); user.Name = txtName.Text; user.Location = txtLocation.Text; _users.Insert(user); var id = user.Id; } else if (rbEntity2.Checked) { var _users = _database.GetCollection <Users2>("users"); var user = new Users2 { }; user.Name = txtName.Text; user.Location = txtLocation.Text; _users.Insert(user); var id = user.Id; } else if (rbEntity3.Checked) { var _users = _database.GetCollection <Users1>("users"); var user = new Users1 { }; user.Age = Convert.ToInt32(txtAge.Text); user.Name = txtName.Text; _users.Insert(user); var id = user.Id; } MessageBox.Show("User with name " + txtName.Text + " created"); }
private void btnFind_Click(object sender, EventArgs e) { _collection = _database.GetCollection <Users3>("users"); IMongoQuery query = Query.EQ("Name", txtName.Text); Users3 _user = _collection.FindAs <Users3>(query).FirstOrDefault(); if (_user != null) { MessageBox.Show(_user.Age.ToString()); } else { MessageBox.Show("Not able to find any results with name " + txtName.Text); } }