0

无法连接到服务器 localhost:27017:
无法建立连接,因为目标机器主动拒绝了它 127.0.0.1:27017。

当我使用 mongoDB 使用 C# 运行控制台应用程序时出现此异常
我已经下载了 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);

      }
 }
4

1 回答 1

0

我会按照这里的指示,在 Mongo 网站上。Windows 快速入门是在 Windows 上开始使用 Mongo 的一个非常好的资源。

至于连接到 .Net 中的 Mongo 实例,如果您在安装 Mongo 期间没有做任何特别的事情,则不必显式提供连接字符串。以下代码适用于我的 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();

        }



    }


}
于 2012-04-26T00:44:20.983 回答