如何使用 MongoDB 的官方 C# 驱动程序检索所有嵌入文档值?

发布于 2024-10-20 09:20:58 字数 1829 浏览 1 评论 0原文

给定以下类和示例文档,如何使用官方 C# 驱动程序从 Question 集合中检索 AnswerChoice 文档,其中 AnswerChoice 中的 _id 为“4d6d336ae0f84c23bc1fae00”。谢谢。

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;}

}

// 示例文档

{
  "_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
    }
}

// 检索 _id 为“4d6d336ae0f84c23bc1fae00”的 AnswerChoice 问题的代码

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);
}

//如何检索 _id 为“4d6d336ae0f84c23bc1fae00”的 AnswerChoice 对象??????

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" ?????

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最舍不得你 2024-10-27 09:20:58

您应该加载问题(如上面的代码所示)并使用 linq 或 foreach 来获取具有指定 _id 的答案项目。所以代码看起来像:

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);
}

另外,我建议使用 FindOne 方法代替 Find(因为我认为您确定只有一个具有上述指定 _id 的答案存在)。

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).

夏日落 2024-10-27 09:20:58

看起来您只需要子文档而不是整个文档。 Mongodb 目前不支持此功能。在匹配时,将返回整个文档。您的查询类似于过滤 MongoDB 中的嵌入文档
此功能请求有一个开放的 JIRA 项目,您应该对其进行投票 http://jira.mongodb .org/browse/SERVER-828

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文