Looking for mongodb-csharp Answers? Try Ask4KnowledgeBase
Looking for mongodb-csharp Keywords? Try Ask4Keywords

mongodb-csharpEmpezando con mongodb-csharp


Observaciones

MongoDB C # Driver es un controlador .NET que maneja las solicitudes al servidor MongoDB. Permite realizar llamadas completamente asíncronas y maneja la serialización / deserificación de objetos utilizando la biblioteca BSON.

MongoDB C # .NET Documentación

Documentación MongoDB C # API

Versiones

Versión Fecha de lanzamiento
2.2.4 2016-05-02
2.2.3 2016-02-02
2.2.2 2016-01-15
2.2.1 2016-01-08
2.2.0 2015-12-08
2.1.1 2015-11-11
2.1.0 2015-10-19
2.0.1 2015-06-08
2.0.0 2015-04-01
1.10.0 2015-01-29

Operaciones básicas

class Program
    {
       
        static void Main(string[] args)
        {
            string serverName = "INTACT-ETL";
            string databaseName = "SOMEDB";
            string collectionName = "TestCol";

            //Build Connection string
            string connectionString = string.Format("mongodb://{0}", serverName);
            //Create MongoClient and connect in one step
            IMongoClient client = new MongoClient(connectionString);
            //Create database object
            IMongoDatabase database = client.GetDatabase(databaseName);

            //Inserting one object
            UserInfo userObject = new UserInfo {FirstName = "Bob",LastName = "Smith", DateOfBirth = DateTime.Now };
            IMongoCollection<UserInfo> collection = database.GetCollection<UserInfo>(collectionName);
            collection.InsertOne(userObject);

            //Retrieving one object
            UserInfo retrievedObject;
            FilterDefinition<UserInfo> theFilter = Builders<UserInfo>.Filter.Eq(p => p.FirstName, "Bob");
            retrievedObject = collection.Find<UserInfo>(theFilter).First();
            Console.WriteLine("FirstName: {0}", retrievedObject.FirstName);
            Console.WriteLine("LastName: {0}", retrievedObject.LastName);
            Console.WriteLine("DateOfBirth: {0}", retrievedObject.DateOfBirth);

            //Delete one object
            DeleteResult result =  collection.DeleteOne<UserInfo>(p => p.FirstName == "Bob");
            Console.WriteLine("Is Acked : {0}",result.IsAcknowledged);
            Console.WriteLine("Press Enter to exit...");
            Console.ReadLine();


        }

        public class UserInfo
        {
            [MongoDB.Bson.Serialization.Attributes.BsonId]
            public ObjectId? _id { get { return ObjectId.GenerateNewId(); } set { } }
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public DateTime DateOfBirth { get; set; }
        }
    }
 

Cómo conectar la base de datos MongoDb desde la aplicación

Definir parametros

private static IMongoClient _client;
private static IMongoDatabase _database;
private static IMongoCollection< -collection class name- > _collection;
 

Asignar valores a los parámetros.

_client = new MongoClient("mongodb://localhost:27017");
_database = _client.GetDatabase("database name here");
_collection = _database.GetCollection< -collection class name- >("collection name here");
 

Instalación o configuración

Descargar el controlador a través de nuget. Usando este comando en la consola del administrador de paquetes

Install-Package mongocsharpdriver