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,但是,今天,当我尝试连接它时,它出现了一个错误:
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 :(得分:2)
我已经解决了这个问题......正如评论所说,将mongodb:// localhost:27017更改为mongodb://127.0.0.1:27017有效..