考虑以下数据
{
"_id" : ObjectId("580bc94b221036257caf2636"),
"Status" : "Scheduled",
//I want to get this Questions list
"Questions" : [
{
"QuestionType" : "Choice",
"Question" : "What is your name?",
"Correct" : "C",
"Options" : [
"xxx",
"xxx",
"xxx"
]
},
{
"QuestionType" : "Choice",
"Question" : "What is your name?",
"Correct" : "C",
"Options" : [
"xxx",
"xxx",
"xxx"
]
}
]}
这是我的查询
var collection = mongodb.GetCollection<QuestionsList>("Papers");
var choice = collection.Find(Query.EQ("userName", "Sohail")).SetFields(Fields.Exclude("status")
.Slice("Questions", 1)).Single();
return choice.multiQuestions.ToList();在过去的一周里,我一直在尝试这个问题,但是似乎没有什么可以解决的,我的问题是如何获得这个问题列表并将其显示给我的MVC视图??谢谢
发布于 2016-10-25 08:57:47
首先,您似乎在查询文档上的用户名字段,而您似乎没有基于上面提供的数据结构:
.Find(Query.EQ("userName", "Sohail"))但假设这是个错误..。
您可以在这里使用$project返回嵌入的问题数组:
var collection = mongodb.GetCollection<QuestionsList>("Papers");
var projection = Builders<QuestionsList>.Projection.Include("Questions");
var choice = collection
.Find(Query.EQ("userName", "Sohail"))
.Project(projection)
.ToList();
return choice;看起来您有一个C#模型(QuestionsList),所以您没有使用强类型查询吗?假设您的模型如下所示:
public class QuestionsList
{
[BsonRepresentation(BsonType.ObjectId)]
[BsonId]
public string Id { get; set; }
//Missing from your data structure but assume it is there
public string userName { get; set; }
public string Status { get; set; }
public List<Question> Questions { get; set; }
}
public class Question
{
public string QuestionType { get; set; }
public string Question { get; set; }
public string Correct { get; set; }
public List<string> Options { get; set; }
}等价物
var choice = collection
.Find(x => x.userName == "Sohail"))
.Project(x => x.Questions)
.ToList();https://stackoverflow.com/questions/40207932
复制相似问题