2016-10-03 66 views
1
using UnityEngine; 
using System; 
using System.Collections; 
using System.Collections.Generic; 
using MongoDB.Bson; 
using MongoDB.Driver; 
using MongoDB.Driver.Builders; 
using MongoDB.Driver.GridFS; 
using MongoDB.Driver.Linq; 
public class DBHandler : MonoBehaviour { 

    string connectionString = "mongodb://localhost:27017"; 
    void Start() { 
     /* 
    * Establish connection 
    */ 
     var client = new MongoClient(connectionString); 
     var server = client.GetServer(); 
     var database = server.GetDatabase("unity"); 
     var shopcollections= database.GetCollection<BsonDocument>("vrshop"); 
     Debug.Log ("Connected!!0.0"); 
     //print all collections 
     foreach (var document in shopcollections.FindAll()) { 
      Debug.Log ("Get all info: \n" + document); 
     } 
    } 
} 

这些代码用来工作,它可以连接到成功的MongoDB,但是,在今天,当我尝试连接它,它出来一个错误:连接的MongoDB用C#团结错误

MongoConnectionException: Unable to resolve host name 'localhost'. 
MongoDB.Driver.MongoServerAddress.ToIPEndPoint (AddressFamily addressFamily) 
MongoDB.Driver.MongoServerInstance.GetIPEndPoint() 
MongoDB.Driver.Internal.MongoConnection.Open() 
MongoDB.Driver.Internal.MongoConnection.GetNetworkStream() 
MongoDB.Driver.Internal.MongoConnection.SendMessage (MongoDB.Bson.IO.BsonBuffer buffer, Int32 requestId) 
Rethrow as MongoConnectionException: Unable to connect to server localhost:27017: Unable to resolve host name 'localhost'.. 
MongoDB.Driver.Internal.DirectMongoServerProxy.Connect (TimeSpan timeout, MongoDB.Driver.ReadPreference readPreference) 
MongoDB.Driver.Internal.DirectMongoServerProxy.ChooseServerInstance (MongoDB.Driver.ReadPreference readPreference) 
MongoDB.Driver.MongoServer.AcquireConnection (MongoDB.Driver.ReadPreference readPreference) 
MongoDB.Driver.MongoCursor`1+MongoCursorConnectionProvider[MongoDB.Bson.BsonDocument].AcquireConnection() 
MongoDB.Driver.Operations.QueryOperation`1[MongoDB.Bson.BsonDocument].GetFirstBatch (IConnectionProvider connectionProvider) 
MongoDB.Driver.Operations.QueryOperation`1[MongoDB.Bson.BsonDocument].Execute (IConnectionProvider connectionProvider) 
MongoDB.Driver.MongoCursor`1[MongoDB.Bson.BsonDocument].GetEnumerator() 
DBHandler.Start() (at Assets/DBHandler.cs:24) 

我相信我使用相同的mongodb ....它有什么问题?

+0

首先检查您的计算机上的任何内容可以作为'localhost'访问。尝试ping它,如果你有web服务器运行尝试访问它在浏览器中作为http:// localhost /等检查防火墙设置,如果你有一个运行。检查“主机”文件是否有奇怪的记录。 – GSazheniuk

+0

是的,我可以从我的Robomongo访问本地主机:27017 ...我想这意味着我可以访问它....但是,我不知道为什么团结? –

+0

如果您正在运行防火墙,可能会有基于应用程序的规则。所以你的统一应用程序可能没有适当的设置来访问本地主机上的27017。如果您的Unity应用程序最近更新了,它可能需要更新防火墙规则。 – GSazheniuk

回答

1

我有固定的......正如评论说,变化的mongodb://本地主机:27017到MongoDB的://127.0.0.1:27017部作品..