用c#连接统一错误的Mongodb
本文关键字:错误 Mongodb 连接 | 更新日期: 2023-09-27 18:11:23
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....怎么了?
我已经修复了…正如评论所说,将mongodb://localhost:27017更改为mongodb://127.0.0.1:27017工作…