无法从我的C#控制台应用程序中使用MongoDB
无法连接到服务器localhost:27017:命令“ping”失败:否>此cmd(响应:{“errmsg”:“无此cmd”,“确定”:0.0})无法从我的C#控制台应用程序中使用MongoDB,c#,mongodb,C#,Mongodb,无法连接到服务器localhost:27017:命令“ping”失败:否>此cmd(响应:{“errmsg”:“无此cmd”,“确定”:0.0}) 这可能是一个基本的东西,我在这里错过了。。。请帮帮我 以上是我得到的例外… 下面是我正在使用的代码(这是站点中给出的示例演示) 注意:我的数据库正在运行。我能够从命令行创建和编辑数据库 using System; using System.Collections.Generic; using MongoDB.Bson; using MongoDB
这可能是一个基本的东西,我在这里错过了。。。请帮帮我 以上是我得到的例外…
下面是我正在使用的代码(这是站点中给出的示例演示) 注意:我的数据库正在运行。我能够从命令行创建和编辑数据库
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);
}
}
}
使用系统;
使用System.Collections.Generic;
使用MongoDB.Bson;
使用MongoDB.Driver;
使用MongoDB.Driver.Builders;
名称空间MongoDBTest
{
公共类实体
{
公共对象Id{get;set;}
公共字符串名称{get;set;}
}
班级计划
{
静态void Main(字符串[]参数)
{
变量连接字符串=”mongodb://localhost/?safe=true";
var server=MongoServer.Create(connectionString);
var database=server.GetDatabase(“测试”);
var collection=database.GetCollection(“实体”);
var entity=新实体{Name=“Tom”};
收款。插入(实体);
var id=entity.id;
var query=query.EQ(“\u id”,id);
实体=collection.FindOne(查询);
entity.Name=“迪克”;
收集、保存(实体);
var update=update.Set(“Name”,“Harry”);
收集、更新(查询、更新);
收集、删除(查询);
}
}
}
从mongo shell可以运行以下命令:
> db.version()
2.2.0
> db.runCommand("ping")
{ "ok" : 1 }
>
这是为了验证您使用的服务器版本是否太旧,以至于没有ping命令。您可以从命令提示符下pinglocalhost吗?可以。我可以从命令提示符ping本地主机。我可以在命令提示符下处理数据库。在连接字符串中将localhost更改为127.0.0.1也没什么区别?我甚至尝试过。问题不是我的应用程序没有ping MongoDB服务器。因为我总是在服务器控制台中说:“连接接受自#地址,在下一行中,我得到作为结束连接#地址”非常感谢Robert。我当时运行的是1.1.0版本:)正在深夜下载文件,我想我得到了最旧的版本。现在它工作得非常好。。。。