How do I update with various c# records with Mongodb?

Asked

Viewed 11 times

-1

Personal I am doing my tcc and I am using Threads to simulate a simultaneous access of users in the database, already managed to do the implementation for mysql as in the example below, but I’m having difficulties to do the same with mongoDB could help me ?

public void testeUpdateMySQL(int numThreads, int numInserts)
    {
        Stopwatch cronometro = new Stopwatch();
        Thread[] th = new Thread[numThreads];

        int salto = numInserts / numThreads;
        int inicioContagem = 1;
        int cont = 0;

        for (cont = 0; cont < numThreads; cont++)
        {
            var inicio = inicioContagem;
            th[cont] = new Thread(() => Sql_Tabela1.UpdateTabela(cont, inicio, salto));
            inicioContagem += salto;
        }

        cronometro.Start();

        for (cont = 0; cont < numThreads; cont++)
        {
            th[cont].Start();
        }

        for (cont = 0; cont < numThreads; cont++)
        {
            th[cont].Join();
        }

        cronometro.Stop();

        Console.WriteLine("UPDATE Tabela1");
        Console.WriteLine("Tempo Decorrido: " + cronometro.Elapsed);
        Console.WriteLine();
    }

 public static void UpdateTabela(int usuario, int inicioContagem, int salto)
    {
        try
        {
            var listTabela1 = Enumerable.Range(inicioContagem, salto)
                .Select(i => new Tabela1()
                {
                    idTabela1 = i,
                    campo1_Tabela1 = -(usuario)
                });

            using (var connection = new MySqlConnection("Server=localhost;DataBase=tcc;Uid=root;Pwd=;"))
            {
                string strQuery = @" UPDATE tabela1     " +
                                   " SET campo1_Tabela1 = @campo1_Tabela1  " +
                                   " Where idTabela1 = @idTabela1          ";
            
                connection.Execute(strQuery, listTabela1);
            }
        }
        catch (Exception error)
        {
            Console.WriteLine(error.Message);
        }
    }

Mongodb example:

public void testeUpdateMongoDB(int numThreads, int numInserts)
    {
        Stopwatch cronometro = new Stopwatch();
        Thread[] th = new Thread[numThreads];

        int salto = numInserts / numThreads;
        int inicioContagem = 1;
        int cont = 0;

        for (cont = 0; cont < numThreads; cont++)
        {
            var inicio = inicioContagem;
            th[cont] = new Thread(() => NoSQL_Tabela1.UpdateTabela(cont, inicio, salto));
            inicioContagem += salto;
        }

        cronometro.Start();

        for (cont = 0; cont < numThreads; cont++)
        {
            th[cont].Start();
        }

        for (cont = 0; cont < numThreads; cont++)
        {
            th[cont].Join();
        }

        cronometro.Stop();

        Console.WriteLine("[MongoDB]: UPDATE Tabela1");
        Console.WriteLine("Tempo Decorrido: " + cronometro.Elapsed);
        Console.WriteLine();
    }
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.