说我有一份文件:
{_id:"5ac79bf31d413a65dbf9427c", username: "Carlos", scores: ["A","A"]}
我如何执行投影以仅检索"得分"从文件?
我试过这样的话:
BsonDocument filter = new BsonDocument("_id", ObjectId.Parse("5ac79bf31d413a65dbf9427c"));
ProjectionDefinition<BsonDocument> projection = Builders<BsonDocument>.Projection.Include("scores").Slice("scores", -1);
BsonDocument document = mongoDB.GetConnection().Find(filter).Project(projection).First().AsBsonDocument;
然而,我仍然得到了整个文件。 任何想法都有帮助,谢谢!
答案 0 :(得分:0)
这对我有用,结果是Slice是不必要的并且会破坏预测:
BsonDocument filter = new BsonDocument("_id", ObjectId.Parse("5ac79bf31d413a65dbf9427c"));
ProjectionDefinition<BsonDocument> projection = Builders<BsonDocument>.Projection.Include("scores");
BsonDocument document = mongoDb.GetConnection().Find(filter).Project(projection).First().AsBsonDocument;