0

我正在尝试从我的 MongoDB 数据库中的集合中返回单个值,但是我得到了一个返回的空值,并且集合中存在具有该 id 的文档。

    public async Task<Business> GetBusiness(string businessId)
    {
        var objectId = new ObjectId(businessId);
        var filter = Builders<Business>.Filter.Eq("_id", objectId);
        var entity = await _businessMongoCollection.Find(filter).FirstAsync();
        return entity;
    }

我也试过这样做,但我得到一个空结果。

var filter = Builders<Business>.Filter.Eq("_id", new ObjectId(businessId));
var foundBusinessTask = await _businessMongoCollection.FindAsync(filter);
var foundBusiness = await foundBusinessTask.SingleOrDefaultAsync();
4

1 回答 1

0

您能否不使用属性说明符而不是字符串“_id”,如下所示:

var filter = Builders<Business>.Filter.Eq(b => b.Id, businessId);

我不认为你需要把 id 变成ObjectId

于 2020-06-05T10:02:54.853 回答