How to retrieve all embedded document value using the official C# driver for MongoDB?

不打扰是莪最后的温柔 提交于 2020-01-14 14:26:11

问题


Given the the following classes and sample document, How do I retrieve a AnswerChoice document from the Question collection where _id in AnswerChoice is '4d6d336ae0f84c23bc1fae00' using the official C# driver. Thank you.

public class Question
{
     [BsonId]
     public ObjectId QuestionId
     {get;set;}

     public string Question
     {get;set;}

     public List<AnswerChoice> AnswerChoices
     {get;set;}
}

public class AnswerChoice
{
     [BsonId]
     public ObjectId AnswerChoiceId
     {get;set;}

     public string Answer
     {get;set;}

     public int Order
     {get;set;}

}

// Sample document

{
  "_id": "4d6d3369e0f84c23bc1facf7",
  "Question": "Question 1",
  "AnswerChoices": [
    {
      "_id": "4d6d3369e0f84c23bc1facf2",
      "Answer": "Answer Choice A",
      "Order": 1
    },
    {
      "_id": "4d6d3369e0f84c23bc1facf3",
      "Answer": "Answer Choice B",
      "Order": 2
    },
    {
      "_id": "4d6d3369e0f84c23bc1facf4",
      "Answer": "Answer Choice C",
      "Order": 3
    },
    {
      "_id": "4d6d3369e0f84c23bc1facf5",
      "Answer": "Answer Choice D",
      "Order": 4
    },
    {
      "_id": "4d6d3369e0f84c23bc1facf6",
      "Answer": "Answer Choice E",
      "Order": 5
    }
}

//Code to retrieve Question that have AnswerChoice with _id of "4d6d336ae0f84c23bc1fae00"

List<Question> list = new List<Question>();
MongoServer _server = MongoServer.Create("mongodb://localhost");
MongoDatabase _database = _server.GetDatabase("test");
var query = Query.And(Query.EQ("AnswerChoices._id", new ObjectId("4d6d336ae0f84c23bc1fae00")));
MongoCollection<Question> collection = _database.GetCollection<Question>("Question");
MongoCursor<Question> cursor = collection.Find(query);

foreach (var q in cursor)
{
    list.Add(q);
}

//How do I retrieve an AnswerChoice object with _id of "4d6d336ae0f84c23bc1fae00" ?????


回答1:


You should load question(as in code above) and use linq or foreach to get answer item with specified _id. So code will looks like:

List<Question> list = new List<Question>();
MongoServer _server = MongoServer.Create("mongodb://localhost");
MongoDatabase _database = _server.GetDatabase("test");
var query = Query.And(Query.EQ("AnswerChoices._id", new ObjectId("4d6d336ae0f84c23bc1fae00")));
MongoCollection<Question> collection = _database.GetCollection<Question>("Question");
MongoCursor<Question> cursor = collection.Find(query);

var id = new ObjectId("4d6d336ae0f84c23bc1fae00");
foreach (var q in cursor)
{
    var answerChoice = q.AnswerChoices.Single(x=> x.AnswerChoiceId == id);
    list.Add(q);
}

Also i suggest instead of Find use FindOne method(because i suppose that you sure that only one answer with above specified _id exists).




回答2:


It looks like you only want the subdocument as opposed to the entire document. This is not currently supported in Mongodb. On a match, the entire document is returned. Your query is similar to Filtering embedded documents in MongoDB. There is an open JIRA item for this feature request which you should vote on http://jira.mongodb.org/browse/SERVER-828



来源:https://stackoverflow.com/questions/5171462/how-to-retrieve-all-embedded-document-value-using-the-official-c-sharp-driver-fo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!