1
我有一個查詢需要通過其「_id」返回產品及其所有信息,但是當我打印結果時,我得到的是遊標而不是MongoDB集合中的值?將BsonDocument轉換爲字符串
var query_id = dbCollection.Find(Query.EQ("_id", "50ed4ea05baffd13a44d0154"));
return query_id.ToString();
結果是
MongoDB.Driver.MongoCursor`1[MongoDB.Bson.BsonDocument]
編輯 我已經試過.ToJason()
方式,但酮基返回[]
var query_id = dbCollection.Find(Query.EQ("_id", "50ed4ea05baffd13a44d0154"));
var r = query_id.ToJson();
編輯 下面是代碼,它的一個基本的Windows窗體應用程序我正在做基本測試:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string connectionString = "mongodb://127.0.0.1";
var server = MongoServer.Create(connectionString);
if (server.State == MongoServerState.Disconnected)
{
server.Connect();
label1.Text = ("Server is connected to: " + connectionString);
button1.Enabled = false;
}
else
{
label1.Text = ("Server Error Check DB location");
}
var conn = server.GetDatabase("Acord");
var dbCollection = conn.GetCollection("Mappings");
var query = dbCollection.FindAll();
//textBox2.Text = query.ToList().ToJson();
var query_first = query.First();
//textBox2.Text = cursorResult.ToString();
var query_id = Query.EQ("_id", "1");//What ID do I put in here? This is were the error is now!
var entity = dbCollection.FindOne(query_id);
textBox2.Text = entity.ToString();
}
}
以下是錯誤我得到:
An unhandled exception of type 'System.NullReferenceException' occurred in WindowsFormsApplication1.exe
我清楚地失去了一些東西?
我得到'未處理的類型'System.NullReferenceException'發生的異常? –
你能提供更多細節嗎?代碼是什麼? –
我已編輯該問題並已添加到整個代碼中 –