我有一个查询需要通过其“_id”返回产品及其所有信息,但是当我打印结果时,我得到的是光标而不是 MongoDB 集合中的值?
var query_id = dbCollection.Find(Query.EQ("_id", "50ed4ea05baffd13a44d0154"));
return query_id.ToString();
结果是
MongoDB.Driver.MongoCursor`1[MongoDB.Bson.BsonDocument]
编辑我已经尝试过该.ToJson()
方法但只返回[]
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
我显然在这里遗漏了什么?