我正在尝试使用以下代码从我的MongoDB中读取一行
public static void MongoConnection()
{
var connectionString = "127.0.0.1";
var mongoClient = new MongoClient(connectionString);
var mongoServer = mongoClient.GetServer();
var databaseName = "PointToPoint";
var db = mongoServer.GetDatabase(databaseName);
var mongodb = db.GetCollection("OCS.MeterEntity");
BsonDocument documentRead = mongodb.FindOne(new QueryDocument {
{"_id", "4B414D000000011613CD" }
});
但是documentRead总是为null,我知道数据库中存在specificik _id。我做错了什么?
它确实连接到数据库和右表。
答案 0 :(得分:1)
似乎是一个召唤惯例:
new QueryDocument( "_id", new ObjectId("4B414D000000011613CD") )
但这也应该有效:
new BSONDocument { { "_id", "4B414D000000011613CD" } }
答案 1 :(得分:0)
我设法弄明白了:)
public static void MongoConnection()
{
var connectionString = "mongodb://localhost";
var mongoClient = new MongoClient(connectionString);
var mongoServer = mongoClient.GetServer();
var databaseName = "PointToPoint";
var db = mongoServer.GetDatabase(databaseName);
var mongodb = db.GetCollection<MongoDBModel>("OCS.MeterEntity");
var mongodbQuery = Query<MongoDBModel>.EQ(x => x._id, "4B414D000000011613CD");
var foundMongoDB = mongodb.FindOne(mongodbQuery);
}