无法连接到服务器 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);
}
}