MongoServer.GetDatabase不返回相同的实例

时间:2014-05-11 11:55:32

标签: mongodb mongodb-.net-driver

根据代码文档MongoServer.GetDatabase

  

获取表示此服务器上的数据库的MongoDatabase实例。   每个数据库组合只创建一个实例   设置。

但是,以下测试失败(尽管数据库名称相同,但我得到了不同的实例):

void describe_get_database()
{
    MongoServer server = null;
    MongoDatabase db = null;
    MongoDatabase db2 = null;
    string dbName = null;

    before = () =>
    {
        var client = new MongoClient("mongodb://localhost");
        server = client.GetServer();

        dbName = "test";
        db = server.GetDatabase(dbName);
    };

    act = () => db2 = server.GetDatabase(dbName);

    context["when the database name is the same"] = () =>
    {
        it["should return the same database instance"] =
            () => db2.should_be_same(db);
    };
}

我误解了文档吗?

1 个答案:

答案 0 :(得分:0)

我同意Ben关于这个问题的观察和质疑。

我甚至在[https://searchcode.com/codesearch/view/26539464/]处挖掘了Mongo c#驱动程序源代码,它没有显示只创建了一个实例的线索。

    /// <summary>
    /// Gets a MongoDatabase instance representing a database on this server. Only one instance
    /// is created for each combination of database settings.
    /// </summary>
    /// <param name="databaseName">The name of the database.</param>
    /// <param name="databaseSettings">The settings to use with this database.</param>
    /// <returns>A new or existing instance of MongoDatabase.</returns>
    public virtual MongoDatabase GetDatabase(string databaseName, MongoDatabaseSettings databaseSettings)
    {
        if (databaseName == null)
        {
            throw new ArgumentNullException("databaseName");
        }
        if (databaseSettings == null)
        {
            throw new ArgumentNullException("databaseSettings");
        }
        return new MongoDatabase(this, databaseName, databaseSettings);
    }