简体   繁体   中英

Error Connecting To MongoDb from C# .NET Console Application

Unable to connect to server localhost:27017:
No connection could be made because the target machine actively refused it 127.0.0.1:27017.

This exception appears when i run a console application with C# using mongoDB
I've downloaded CSharpDriver-1.4.1.4490.msi

 using System;
  using System.Collections.Generic;
 using System.Linq;
using System.Text;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;


namespace ConsoleApplication4
{
    public class Entity
   {
       public ObjectId Id { get; set; }
       public string Name { get; set; }
    }

class Program
{
    static void Main(string[] args)
    {

        var connectionString = "mongodb://localhost:27017"; 
            var server = MongoServer.Create(connectionString);
            var database = server.GetDatabase("test");
            var collection = database.GetCollection<Entity>("entities");

            var entity = new Entity { Name = "Tom" };
            collection.Insert(entity);
            var id = entity.Id;

            var query = Query.EQ("_id", id);
            entity = collection.FindOne(query);

            entity.Name = "Dick";
            collection.Save(entity);

            var update = Update.Set("Name", "Harry");
            collection.Update(query, update);

            collection.Remove(query);

      }
 }

I would follow the directions here, on the Mongo site. Windows quickstart is a really good resource to get started using Mongo on windows.

As far as connecting to the Mongo instance in .Net, if you didn't do anything special during the installation of Mongo, you shouldn't have to explicitly give a connection string. The following code works for my generic set up of Mongo.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Bson.Serialization.Conventions;
using MongoDB.Bson.Serialization.IdGenerators;
using MongoDB.Bson.Serialization.Options;
using MongoDB.Bson.Serialization.Serializers;
using MongoDB.Driver.Builders;
using MongoDB.Driver.GridFS;
using MongoDB.Driver.Wrappers;

namespace MongoDB
{
    class Program
    {
        static void Main(string[] args)
        {
            MongoServer server;
            MongoDatabase moviesDb;

            server = MongoServer.Create();
            moviesDb = server.GetDatabase("movies_db");

            //Create some data
            var movie1 = new Movie { Title = "Indiana Jones and the Raiders of the Lost Ark", Year = "1981" };
            movie1.AddActor("Harrison Ford");
            movie1.AddActor("Karen Allen");
            movie1.AddActor("Paul Freeman");

            var movie2 = new Movie { Title = "Star Wars: Episode IV - A New Hope", Year = "1977" };
            movie2.AddActor("Mark Hamill");
            movie2.AddActor("Harrison Ford");
            movie2.AddActor("Carrie Fisher");

            var movie3 = new Movie { Title = "Das Boot", Year = "1981" };
            movie3.AddActor("Jürgen Prochnow");
            movie3.AddActor("Herbert Grönemeyer");
            movie3.AddActor("Klaus Wennemann");

            //Insert the movies into the movies_collection
            var moviesCollection = moviesDb.GetCollection<Movie>("movies_collection");
            //moviesCollection.Insert(movie1);
            //moviesCollection.Insert(movie2);
            //moviesCollection.Insert(movie3);

            var query = Query.EQ("Year","1981");

            var movieFound = moviesDb.GetCollection<Movie>("movies_collection").Drop();

        }



    }


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM