& # 39; MongoDB.Driver.MongoClient& # 39;不包含'GetServer
本文关键字:GetServer 包含 Driver MongoDB MongoClient | 更新日期: 2023-09-27 18:16:32
我正试图熟悉从c#程序编写到MongoDB。我按照http://mongodb.github.io/mongo-csharp-driver/1.11/getting_started/
的建议设置了我的代码我正在尝试运行这个程序,但得到这个错误"'MongoDB.Driver. exe '。MongoClient'不包含'GetServer'的定义,也没有扩展方法'GetServer'接受类型为'MongoDB.Driver '的第一个参数。MongoClient' could be found"。能帮我一下吗?
提前感谢,Tien .
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;
//Additionally, you will frequently add one or more of these using statements:
//using MongoDB.Driver.Builders; //Error rebuilding when this statement is active: "Using the generic type 'MongoDB.Driver.Builders<TDocument>' requires 1 type arguments
//using MongoDB.Driver.GridFS;
using MongoDB.Driver.Linq;
//using MongoDB.Driver.MongoClient; //Error rebuilding when this statement is active "A using namespace directive can only be applied to namespaces; 'MongoDB.Driver.MongoClient' is a type not a namespace
namespace write2MongoDb
{
public class Entity
{
public ObjectId Id { get; set; }
public string Name { get; set; }
}
class Program
{
static void Main(string[] args)
{
#region Full Sample Program
var connectionString = "mongodb://localhost";
var client = new MongoClient(connectionString);
var server = client.GetServer();
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<Entity>.EQ(e => e.Id, id);
entity = collection.FindOne(query);
entity.Name = "Dick";
collection.Save(entity);
var update = Update<Entity>.Set(e => e.Name, "Harry");
collection.Update(query, update);
collection.Remove(query);
#endregion
Console.ReadKey();
}
}
}
GetServer()已弃用,从客户端检索数据库,如下所示:
var client = new MongoClient("mongodb://localhost");
var database = client.GetDatabase("test");
var collection = database.GetCollection<Entity>("entities");