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
.