我需要先通过 _id 找到该文档。 然后在文档中查找具有Time字段的子文档大于参数 lastTime
var filter = builder.Eq("_id", symbol) & builder.Gt("Update.Time", lastTime);
var result = await MongoDb.CollectionName.Find(filter).ToListAsync();
此示例的结果为0.
如何编写此查询?我需要得到这个子文档"更新"或最后3个子子文件
该文件具有以下结构
{
{"_id", symbol},
{"Update", [
{"_id", number}, {"Time", sometime}, {"Version", versionNumber},
{"_id", number}, {"Time", sometime}, {"Version", versionNumber},
{"_id", number}, {"Time", sometime}, {"Version", versionNumber},
{"_id", number}, {"Time", sometime}, {"Version", versionNumber},}
]
}
答案 0 :(得分:0)
如果mongodb控制台会起作用的话:
> db.doc.save({
"_id":"123",
"update":[
{"_id":'1', "time":'345', "Version":'3'},
{"_id":'2', "time":'234', "Version":'4'}
]
})
> db.doc.findOne( { _id:"123", "update.time":{ $gt: 344 } } )
那么对于C#你可以试试吗?
var collection = _database.GetCollection<BsonDocument>("name");
var filter = Builders<BsonDocument>.Filter.Gt("update.time", 344);
var result = await collection.Find(filter).ToListAsync();
UPD这可以是:
var collection = _database.GetCollection<BsonDocument>("name");
var builder = Builders<BsonDocument>.Filter;
var filter = builder.Eq("_id", id) & builder.Gt("update.time", 344);
var result = await collection.Find(filter).ToListAsync();
答案 1 :(得分:-1)
尝试使用lambda表达式:
var lastTime = 1;
var result = collection.Find(x => x.Update.Contains(lastTime));