Advertisement

Unity3D连接Mongodb数据库

阅读量:

第一步:在Assets中新建Plugins文件夹。

在第二步中,请使用DOS命令行工具安装 MongoDB.Driver,并运行以下命令:dotnet add package MongoDB.Driver --version 2.7.0

第三步骤是获取这些dynamic_links_库。将这些dynamic_links_库存放于 Plugins 文件夹内。请下载以下连接中的资源:提取码为 yrdd 的是 https://pan.baidu.com/s/1iWKFgIL7lApcJ-IvOK129g

第四步:写脚本,挂在游戏对象上即可。

复制代码
 using System.Collections;

    
 using System.Collections.Generic;
    
 using UnityEngine;
    
 using MongoDB.Bson;
    
 using MongoDB.Driver;
    
  
    
  
    
  
    
 public class MongodbTest : MonoBehaviour
    
 {
    
     // Start is called before the first frame update
    
     void Start()
    
     {
    
     InsertData();
    
     }
    
  
    
     private IMongoCollection<BsonDocument> DatabaseConn()
    
     {
    
  
    
     var client = new MongoClient("mongodb://localhost");
    
     var database = client.GetDatabase("test"); //数据库名称
    
     var collection = database.GetCollection<BsonDocument>("WAWA");//连接的表名
    
  
    
     return collection;
    
     }
    
  
    
     public void InsertData()
    
     {
    
     var document = new BsonDocument{
    
     {"aa",11},
    
     {"bb",22},
    
     {"cc",new BsonDocument{
    
         {"x",33},
    
         {"y",44}
    
     }}
    
     };
    
     var collection = DatabaseConn();
    
     collection.InsertOne(document);
    
     }
    
 }

全部评论 (0)

还没有任何评论哟~