gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:57:26 25 4
gpt4 key购买 nike

我尝试将MongoDB与this unity tutorial一起应用。MongoDB购买了mLab,因此通过mongoDB map 集完成了mongoDB的登录。而且它们仅支持MongoDB 3.6及更高版本。

我已经使用 AWS 创建了集群(类似于GUI mLab拥有的集群),

  • 在“概述”选项卡上,单击“连接”按钮。在以下三个选项中:“与Mongo Shell连接”/“连接您的应用程序”/“与MongoDB Compass连接”-连接您的应用程序与您提供的代码最相关。
  • 在:“连接您的应用程序”中,您需要选择一个驱动程序:我选择了C#/.NET(因为这是一个C#项目),版本为2.5或更高版本。
  • 有“连接字符串”:mongodb+srv://user_name:<password>@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帐户
  • 的用户
  • 运行“服务器”场景时出现错误:=
  • ArgumentException: Invalid keyword 'mongodb+srv://:@serverclientmp-
    1oqao.gcp.mongodb.net/test?retrywrites'.
    MongoDB.Driver.MongoConnectionStringBuilder.set_Item (System.String
    keyword, System.Object value)
    System.Data.Common.DbConnectionStringBuilder.ParseConnectionStringNonOdbc
    (System.String connectionString)
    System.Data.Common.DbConnectionStringBuilder.ParseConnectionString
    (System.String connectionString)
    System.Data.Common.DbConnectionStringBuilder.set_ConnectionString
    (System.String value) MongoDB.Driver.MongoConnectionStringBuilder..ctor
    (System.String connectionString)
    MongoDB.Driver.MongoClient.ParseConnectionString (System.String
    connectionString) MongoDB.Driver.MongoClient..ctor (System.String
    connectionString) Mongo.Init () (at Assets/Scripts/Database/Mongo.cs:16)
    Server.Init () (at Assets/Scripts/Server.cs:40) Server.Start () (at
    Assets/Scripts/Server.cs:28)

    我尝试将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)

    我想知道要添加到脚本中才能使其正常工作,或者本教程是否过时并且需要完成新方法。

    谢谢

    最佳答案

    我知道我已经晚了8个月,但是我最近开始在Unity中使用MongoDB,我不知道这是否会有所帮助,但是简短的答案是我不确定。但是我设法连接到我的远程和本地数据库,因此下面将说明我是如何做到的。

    长答案:
    我没有观看您跟随的教程视频,但是我设置了这些dll files的连接(我假设您还为它们创建了插件文件夹)。至于“教程”,我仅遵循了此快速入门guideone。与API相关的所有其他内容,我都只读了documentation

  • 我正在使用 MongoDB v4.2.1
  • 我正在使用 Unity 2019.3
  • 我正在使用 API兼容性.Net Standard 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;
    }

    关于c# - 我可以在Unity 2018.4中使用MongoDB 3.6或更高版本吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58452315/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com