Mongo DB does not connect to C # .NET

1

I have a project in C # .NET in which I referenced the necessary drivers to use mongo in this language, until then, everything is correct. However, there is no type of reaction on the part of the database when I execute the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MongoDB.Bson;
using MongoDB.Driver;
using System.Configuration;
using System.Threading.Tasks;

public partial class _Default : System.Web.UI.Page
{
    protected static IMongoClient _client;
    protected static IMongoDatabase _database;
    protected async Task Page_Load(object sender, EventArgs e)
    {
        var connectionString = ConfigurationManager.ConnectionStrings["MongoConnectionStr"].ConnectionString;
        //var mongoUrl = MongoUrl.Create(connectionString);

        _client = new MongoClient(connectionString);
        _database = _client.GetDatabase("LinkedInDB");

        await insertarDato();
    }

    public async Task insertarDato()
    {
        var document = new BsonDocument
        {
            { "address" , "direccion" }
        };

        var collection = _database.GetCollection<BsonDocument>("users");
        await collection.InsertOneAsync(document);

    }

}

In the Web.config I have the following connection string:

  <connectionStrings>
    <add name="MongoConnectionStr" 
      connectionString="mongodb://127.0.0.1:5665/"
        providerName="MongoConnectionStr"/>
  </connectionStrings>  

I start mongod with the port I say in the connection string, that is, the 5665 with: mongod --port 5665 . Then I connect the database to the same port with: mongo --port 5665 to verify if there was any change after running the Web project, but nothing happens. I already tried to change 127.0.0.1 by localhost .

    
asked by Parzival 21.10.2017 в 14:32
source

2 answers

2

Download the packages from the Nuget and install version 2.4.4, select the MongoDB.Driver.GridFS this will install the packages needed , the mongo ran alone with

  

Mongod

and verify your status with Robomongo

The code c # is as follows:

using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class _Default : Page
    {
        protected static IMongoClient clienteLocal = new MongoClient();
        protected static IMongoDatabase dataLocal = clienteLocal.GetDatabase("base");
        protected void Page_Load(object sender, EventArgs e)
        {
            insertar();
        }
        public void insertar() {
            string id = ObjectId.GenerateNewId().ToString();
            var documento = new BsonDocument
                            {
                                { "_id",id},
                                { "deviceId","123"},
                                { "date", DateTime.Now},
                                { "status", "Desconectado" }
                            };
            var collection_ = dataLocal.GetCollection<BsonDocument>("coleccion");
            collection_.InsertOneAsync(documento);
        }
        public void buscar(id) {
            var collection_ = dataLocal.GetCollection<BsonDocument>("coleccion");
            var filtro = Builders<BsonDocument>.Filter.Eq("_id", id);
            var datos = collection_.Find<BsonDocument>(filtro).FirstOrDefault();
            string nombre = datos["status"].ToString();
        }
        public void actualizar(id) {
            var res = dataLocal.GetCollection<BsonDocument>("coleccion");
            var filter = Builders<BsonDocument>.Filter.Eq("_id", id);
            var update = Builders<BsonDocument>.Update.Set("status", "Conectado");
            var result = res.UpdateOneAsync(filter, update);
        }
        public void eliminar(id){        
            var collection = dataLocal.GetCollection<BsonDocument>("coleccion");
            var filter = Builders<BsonDocument>.Filter.Eq("_id", id);
            var cuantos= collection.Find<BsonDocument>(filter).Count();
            if (cuantos == 1) { var delete = collection.DeleteManyAsync(filter); }     
        }
    }
}

leaving the data as follows

    
answered by 24.10.2017 / 16:02
source
0

The problem is that since you have changed the Page_Load you will see that it is not entering you because you have declared it async.

Change the Load for this:

protected void Page_Load(object sender, EventArgs e)
        {

            _client = new MongoClient(ConnectionString);
            _database = _client.GetDatabase("LinkedInDB");

            Task.Run(() => insertarDato());
        }

This is how you should insert it correctly.

    
answered by 24.10.2017 в 13:35