SELECT WHERE in MongoDB and C #

0

I have a question about MongoDB and C #, I need to filter the searches by an employee's name, that is, you put your name in a text box and if it exists the data is displayed in DataGrid .

These are my advances:

public class Entity
{
    public ObjectId Id { get; set; }
    public string nombre { get; set; }
    public string apepat { get; set; }
    public string apemat { get; set; }
}

private void button1_Click(object sender, EventArgs e)
{
    var connectionString = "mongodb://localhost";
    var client = new MongoClient(connectionString);
    var server = client.GetServer();
    var database = server.GetDatabase("base_datos");
    var collection = database.GetCollection<Entity>("empleados");
    BindingList<Entity> doclist = new BindingList<Entity>();
    foreach (var deger in collection.Find("nombre", textBox1.Text))
    {
        doclist.Add(deger);
        string[] row0 = new string[] { deger.Id.ToString() };
        string[] row2 = new string[] { deger.nombre.ToString() };
        string[] row3 = new string[] { deger.apepat.ToString() };
        string[] row4 = new string[] { deger.apemat.ToString() };
        Application.DoEvents();
        dataGridView1.DataSource = doclist;
    }
}
    
asked by Rastalovely 13.10.2016 в 18:38
source

2 answers

0

This was the way I solved my problem:

With the New version of the driver:

protected static IMongoClient cliente = new MongoClient();
protected static IMongoDatabase dataLocal = cliente.GetDatabase("base");
  

In this part it is no longer necessary to define the class of'tutabla'

var collection= dataLocal.GetCollection<BsonDocument>("tutabla");
var filter = Builders<BsonDocument>.Filter.Eq("_id", ab);
  

Filter is the equivalent of where in the SQL bases, where ab is the element to look for in the _id field of your tutabla collection

Data has in a list all the records that meet the condition of the cycle

var datos= collection.Find<BsonDocument(filter).SetLimit(1).ToList();

In this part we can do'Counts', jumps or limits as it is' SQL'

var datos= collection.Find<BsonDocument(filter).Skip(0).Limit(10).ToList();

Finally to solve the Question:

 foreach (var item in datos)
  {
   //dataGrid Add(name);
  }

Or through the Second Form:

   void cargargrilla()
    {          
        var conn = "mongodb://localhost";
        var client = new MongoClient(conn );
        var server = client.GetServer();
        var database = server.GetDatabase("db");
        var collection = database.GetCollection<Empleados>("empleados");
        BindingList<Empleados> doclist = new BindingList<Empleados>();
        var query = Query.EQ("_id", idporfiltrar);
        var datos= collection .FindAs<BsonDocument (query).SetLimit(1).ToList();
        foreach (var deger in datos())
        {
            doclist.Add(deger);

            string[] row1 = new string[] { deger.nombre.ToString() };
            string[] row2 = new string[] { deger.descripcion.ToString() };
            string[] row3 = new string[] { deger.localidad.ToString() };
            Application.DoEvents();
            dataGridView1.DataSource = doclist;
        }

        dataGridView1.Columns[1].HeaderText = "IDD";
        dataGridView1.Columns[1].Visible = false;
        dataGridView1.Columns[1].Name = "ide";
        dataGridView1.Columns[2].HeaderText = "Nombre";
        dataGridView1.Columns[2].Name = "name";
        dataGridView1.Columns[3].HeaderText = "Descripcion";
        dataGridView1.Columns[3].Name = "descripcion";
        dataGridView1.Columns[4].HeaderText = "Localidad";
        dataGridView1.Columns[4].Name = "localidad";

    }
    
answered by 08.02.2017 / 04:56
source
0

According to the GetCollection documentation, it returns a MongoCollection, in which you correctly call its Find function, but this function according to the documentation receives an IMongoQuery parameter, which you can create using QueryBuilder

    
answered by 13.10.2016 в 19:50