无法从我的C#控制台应用程序中使用MongoDB

时间:2012-09-07 11:46:25

标签: c# mongodb

  

无法连接到服务器localhost:27017:命令'ping'失败:否>这样的cmd(响应:{“errmsg”:“没有这样的cmd”,“ok”:0.0})。


这可能是我在这里遗漏的基本内容......请帮帮我

以上是我得到的例外......
下面是我正在使用的代码(这是网站上给出的示例演示) 注意:我的数据库正在运行。我可以从命令行创建和编辑数据库。

using System;
using System.Collections.Generic;

using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;

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

    class Program
    {
        static void Main(string[] args)
        {
            var connectionString = "mongodb://localhost/?safe=true";
            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);
        }
    }
}

1 个答案:

答案 0 :(得分:4)

从mongo shell可以运行以下命令:

> db.version()
2.2.0
> db.runCommand("ping")
{ "ok" : 1 }
>

这是为了验证您是否使用的服务器版本太旧而没有ping命令。

相关问题