C# 我可以在Unity 2018.4中使用MongoDB 3.6或更高版本吗?

C# 我可以在Unity 2018.4中使用MongoDB 3.6或更高版本吗?,c#,database,mongodb,unity3d,compiler-errors,C#,Database,Mongodb,Unity3d,Compiler Errors,我正试图用MongoDB购买的mLab应用MongoDB,所以MongoDB的签名是通过MongoDB atlas完成的。它们只支持MongoDB 3.6及更高版本 我已经用AWS创建了一个集群(类似于GUI mLab) 在“概述”选项卡上点击“连接”按钮。在三个选项中:“连接Mongo Shell”/“连接您的应用程序”/“连接MongoDB Compass”-连接您的应用程序看起来与您提供的代码最相关 在“连接你的应用程序”中,你需要选择一个驱动程序:我选择了C#/.NET(因为这是一个C

我正试图用MongoDB购买的mLab应用MongoDB,所以MongoDB的签名是通过MongoDB atlas完成的。它们只支持MongoDB 3.6及更高版本

我已经用AWS创建了一个集群(类似于GUI mLab)

  • 在“概述”选项卡上点击“连接”按钮。在三个选项中:“连接Mongo Shell”/“连接您的应用程序”/“连接MongoDB Compass”-连接您的应用程序看起来与您提供的代码最相关
  • 在“连接你的应用程序”中,你需要选择一个驱动程序:我选择了C#/.NET(因为这是一个C#项目),版本2.5或更高版本
  • 获取“连接字符串”:
    mongodb+srv://user_name:@testmp pkump.mongodb.net/test?retryWrites=true
几件事:

  • 项目名称不是“test”(正如这里所说,就在retryWrites=true之前):mongodb+srv://:@testmp pkump.mongodb.net/test?retryWrites=true

  • 我已将此链接传递到以下代码:private const string MONGO_URI,对于用户名,我写入已添加到集群中的用户,对于密码,我写入已添加到集群中的用户的密码。-而不是我用来创建MongoDB帐户的用户

  • 运行“服务器”场景会解决一个错误:=

  • 我试图将API兼容性级别更改为.NET 4.x,但出现以下错误:

    ArgumentException: Invalid option 'retryWrites'.
    Parameter name: url
    MongoDB.Driver.MongoUrlBuilder.Parse (System.String url) (at 
    <6da29fc855c44d33ad78b3e27475ff27>:0)
    MongoDB.Driver.MongoUrlBuilder..ctor (System.String url) (at 
    <6da29fc855c44d33ad78b3e27475ff27>:0)
    MongoDB.Driver.MongoUrl..ctor (System.String url) (at 
    <6da29fc855c44d33ad78b3e27475ff27>:0)
    MongoDB.Driver.MongoClient.ParseConnectionString (System.String 
    connectionString) (at <6da29fc855c44d33ad78b3e27475ff27>:0)
    MongoDB.Driver.MongoClient..ctor (System.String connectionString) (at 
    <6da29fc855c44d33ad78b3e27475ff27>:0)
    Mongo.Init () (at Assets/Script/Database/Mongo.cs:15)
    Server.Init () (at Assets/Script/Server.cs:38)
    Server.Start () (at Assets/Script/Server.cs:27)
    
    ArgumentException:无效选项“retryWrites”。
    参数名称:url
    MongoDB.Driver.MongoUrlBuilder.Parse(System.String url)(位于
    :0)
    MongoDB.Driver.MongoUrlBuilder..ctor(System.String url)(位于
    :0)
    MongoDB.Driver.MongoUrl..ctor(System.String url)(位于
    :0)
    MongoDB.Driver.MongoClient.ParseConnectionString(System.String
    连接字符串(位于:0)
    MongoDB.Driver.MongoClient..ctor(System.String connectionString)(位于
    :0)
    Init()(位于Assets/Script/Database/Mongo.cs:15)
    Init()(位于Assets/Script/Server.cs:38)
    Server.Start()(位于Assets/Script/Server.cs:27)
    
    我想知道我需要添加什么到我的脚本,使其工作,或者如果这个教程是过时的,需要做一个新的方法


    谢谢

    我知道我已经晚了8个月,但我最近开始在Unity中与MongoDB合作,我不知道这是否会有帮助,但简短的回答是,我不确定。但是我成功地连接到了我的远程和本地数据库,所以我将在下面解释我是如何做到的


    长答案:

    我没有观看您观看的教程视频,但我设置了与这些插件的连接(我假设您也为它们创建了一个插件文件夹)。至于“教程”,我只是简单地遵循了这个快速开始和这个。其他所有与API相关的内容我都读了

    • 我使用的是MongoDBv4.2.1
    • 我正在使用Unity 2019.3
    • 我正在使用API兼容性.Net标准2.0
    • 我正在集群上使用管理员用户。我不知道你的情况是什么,但我记得文档中说你的用户应该有读写权限,所以请检查一下
    最后,当我第一次尝试连接到远程数据库时,我出现了一个错误,但可能是因为我的代码包装在一个try-catch块中,它没有产生您提供的错误,而是:

    Unable to authenticate using sasl protocol mechanism SCRAM-SHA-1 mongo c# driver
    
    这是因为我忘了把尖括号从代码中去掉,取而代之的是--是的,我把我的密码包在括号里了,菜鸟错了,哈哈


    以下是我连接到Atlas群集的方式:

    private const string remoteDB = "mongodb+srv://user_name:<password>@mycluster.mongodb.net/test?retryWrites=true";
    static MongoClient newClient = new MongoClient(remoteDB);
    
    internal Database()//This is a constructor, I'm not using MonoBehaviour
    {
        db = newClient.GetDatabase(DATABASE_NAME);
        lbCollection = db.GetCollection<BsonDocument>(lbColl);
        gameStateCollection = db.GetCollection<BsonDocument>(gameStateColl);
    }
    
    internal static async Task TestConnection()
    {
        try
        {
            await GetDBNames();
        }
        catch (Exception e)
        {
            Debug.Log("<color=red> Error</color> found while testing db connection:");
            Debug.Log(e.Message);
        }
    }
    
    private static async Task<List<string>> GetDBNames()
    {
        List<string> dbNames = new List<string>();
    
        var dbs = await newClient.ListDatabaseNamesAsync();
        dbNames = dbs.ToList();
    
        if (dbNames.Count > 0)
        {
            Debug.Log($"Databases in {nameof(dbNames)}: ");
    
            for (int i = 0; i < dbNames.Count; i++)
            {
                Debug.Log($"{dbNames[i]}");
            }
            
            return dbNames;
        }
    
        Debug.Log($"<color=red>{nameof(GetDBNames)}</color> Returning null. No Dbs found.");
        return null;
    }
    
    private const string remoteDB=“mongodb+srv://user_name:@mycluster.mongodb.net/test?retryWrites=true”;
    静态MongoClient newClient=新MongoClient(remoteDB);
    内部数据库()//这是一个构造函数,我没有使用MonoBehavior
    {
    db=newClient.GetDatabase(数据库名称);
    lbCollection=db.GetCollection(lbColl);
    gameStateCollection=db.GetCollection(gameStateColl);
    }
    内部静态异步任务TestConnection()
    {
    尝试
    {
    等待GetDBNames();
    }
    捕获(例外e)
    {
    Log(“测试数据库连接时发现错误:”);
    Debug.Log(e.Message);
    }
    }
    私有静态异步任务GetDBNames()
    {
    List dbNames=new List();
    var dbs=await newClient.ListDatabaseNamesAsync();
    dbNames=dbs.ToList();
    如果(dbNames.Count>0)
    {
    Log({nameof(dbNames)}:“$”中的数据库);
    for(int i=0;i
    private const string remoteDB = "mongodb+srv://user_name:<password>@mycluster.mongodb.net/test?retryWrites=true";
    static MongoClient newClient = new MongoClient(remoteDB);
    
    internal Database()//This is a constructor, I'm not using MonoBehaviour
    {
        db = newClient.GetDatabase(DATABASE_NAME);
        lbCollection = db.GetCollection<BsonDocument>(lbColl);
        gameStateCollection = db.GetCollection<BsonDocument>(gameStateColl);
    }
    
    internal static async Task TestConnection()
    {
        try
        {
            await GetDBNames();
        }
        catch (Exception e)
        {
            Debug.Log("<color=red> Error</color> found while testing db connection:");
            Debug.Log(e.Message);
        }
    }
    
    private static async Task<List<string>> GetDBNames()
    {
        List<string> dbNames = new List<string>();
    
        var dbs = await newClient.ListDatabaseNamesAsync();
        dbNames = dbs.ToList();
    
        if (dbNames.Count > 0)
        {
            Debug.Log($"Databases in {nameof(dbNames)}: ");
    
            for (int i = 0; i < dbNames.Count; i++)
            {
                Debug.Log($"{dbNames[i]}");
            }
            
            return dbNames;
        }
    
        Debug.Log($"<color=red>{nameof(GetDBNames)}</color> Returning null. No Dbs found.");
        return null;
    }