Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何通过C驱动程序查询MongoDB中的嵌入式文档?_C#_Linq_Mongodb - Fatal编程技术网

C# 如何通过C驱动程序查询MongoDB中的嵌入式文档?

C# 如何通过C驱动程序查询MongoDB中的嵌入式文档?,c#,linq,mongodb,C#,Linq,Mongodb,我想知道如何通过1.7版在MongoDB集合中查询数组中的嵌入文档?通过查询嵌入的文档,我的意思是我只想检索嵌入的文档,而不是包含它的文档 我正在查询的数据模型示例,带有嵌入式文档: // Library, contained in db.Libraries { _id: 1, Categories: [{_id: 2, Name: "Classics", Books: [{_id: 3, Name: The Count of Monte Cristo}]}] } 这里的问题是

我想知道如何通过1.7版在MongoDB集合中查询数组中的嵌入文档?通过查询嵌入的文档,我的意思是我只想检索嵌入的文档,而不是包含它的文档

我正在查询的数据模型示例,带有嵌入式文档:

// Library, contained in db.Libraries
{
    _id: 1,
    Categories: [{_id: 2, Name: "Classics", Books: [{_id: 3, Name: The Count of Monte Cristo}]}]
}
这里的问题是如何查询库集合中id为1的对象和id为3的其中一本书,并只返回该书。这完全可行吗?据我所知,可以通过MongoDB外壳中的投影来实现这一点

我的示例查询是查询db.Libraries,以查找包含在具有_id1的库中的具有_id3的书籍。返回的图书将是以下子文档:

{_id: 3, Name: The Count of Monte Cristo}]}
我已经看了这个问题,但我不能让被接受的答案对我有用

编辑:

我现在可以看到,人们接受的答案是,某种程度上。我没有看到它会迭代找到的每个文档,相当于:

var libraryCollection = new MongoCollection<Library>();
var refBook = new Book { Id = ObjectId.GenerateNewId().ToString(), Name = "The Count of Monte Cristo" };
libraryCollection.Insert(new Library { Id = ObjectId.GenerateNewId().ToString(), Categories = new[] { new Category { Books = new[] { refBook } } } });

MongoCursor<Library> libraries = libraryCollection.Find(new QueryDocument(
                    new Dictionary<string, object>
                        {
                            {"_id", new ObjectId()},
                        }
                    ));
Book book;
foreach (var library in libraries)
{
    book = library.Categories.SelectMany(c => c.Books).FirstOrDefault(b => b.Id == refBook.Id);
}

然而,这个解决方案是没有意义的,因为它检索整个图书馆文档,而不仅仅是嵌入的图书文档。我真的是在必须只反序列化嵌入的书籍(AKA)之后。要执行投影,其中生成的文档不仅会被过滤,而且会被重塑,您需要使用以下查找方法,而不是像这样的查找方法之一:

var result = collection.Aggregate(
    // Only include the document where _id = 1
    new BsonDocument {{"$match", new BsonDocument {{"_id", 1}}}},
    // 'Unwind' the Categories array by duplicating the docs, one per element.
    new BsonDocument {{"$unwind", "$Categories"}},
    // Now do the same for the Books array inside each Categories element.
    new BsonDocument {{"$unwind", "$Categories.Books"}},
    // Only include the resulting docs with a Book _id of 3
    new BsonDocument {{"$match", new BsonDocument {{"Categories.Books._id", 3}}}},
    // Reshape the document to bring the book attributes out to the top level 
    new BsonDocument {{"$project", new BsonDocument {
        {"_id", "$Categories.Books._id"},
        {"Name", "$Categories.Books.Name"}
    }}}
);
result.Response.toJson:


要在生成的文档不仅经过过滤,而且经过重塑的情况下执行投影,您需要使用以下查找方法,而不是使用其中一种方法:

var result = collection.Aggregate(
    // Only include the document where _id = 1
    new BsonDocument {{"$match", new BsonDocument {{"_id", 1}}}},
    // 'Unwind' the Categories array by duplicating the docs, one per element.
    new BsonDocument {{"$unwind", "$Categories"}},
    // Now do the same for the Books array inside each Categories element.
    new BsonDocument {{"$unwind", "$Categories.Books"}},
    // Only include the resulting docs with a Book _id of 3
    new BsonDocument {{"$match", new BsonDocument {{"Categories.Books._id", 3}}}},
    // Reshape the document to bring the book attributes out to the top level 
    new BsonDocument {{"$project", new BsonDocument {
        {"_id", "$Categories.Books._id"},
        {"Name", "$Categories.Books.Name"}
    }}}
);
result.Response.toJson:


当你提供的链接中被接受的答案确实显示了比你提供的代码多得多的代码时,你怎么能指望有人帮助你呢。。请显示所有与您的问题相关的代码,否则您会要求我们保持警惕Readers@DJKRAZE没必要粗鲁。问题本身应该从我提供的数据模型和我查询的内容(我也已经说明)中清楚地看到。你能具体说明缺少什么类型的代码来解释问题吗?我不是在粗鲁,我是在要求你展示你在尝试执行查询时使用的代码。。您只显示了看似JSON脚本的内容。@DJKRAZE您称之为数据模型的MongoDB表示形式的JSON脚本。如果我知道如何用C编写所需的查询,我就不必问。。。MongoDB中的数据预测并不是微不足道的,因为在C中它们在某个点上是不可能的,但我希望它们现在是这样。你是说这样的东西不会起作用吗。。?var query=query.AndQuery.EQBooks.\u id,新object1;MongoCollection collection=\u database.GetCollectionBooks;MongoCursor=collection.Findquery;当你提供的链接中被接受的答案确实显示了比你提供的代码多得多的代码时,你怎么能指望有人帮助你呢。。请显示所有与您的问题相关的代码,否则您会要求我们保持警惕Readers@DJKRAZE没必要粗鲁。问题本身应该从我提供的数据模型和我查询的内容(我也已经说明)中清楚地看到。你能具体说明缺少什么类型的代码来解释问题吗?我不是在粗鲁,我是在要求你展示你在尝试执行查询时使用的代码。。您只显示了看似JSON脚本的内容。@DJKRAZE您称之为数据模型的MongoDB表示形式的JSON脚本。如果我知道如何用C编写所需的查询,我就不必问。。。MongoDB中的数据预测并不是微不足道的,因为在C中它们在某个点上是不可能的,但我希望它们现在是这样。你是说这样的东西不会起作用吗。。?var query=query.AndQuery.EQBooks.\u id,新object1;MongoCollection collection=\u database.GetCollectionBooks;MongoCursor=collection.Findquery;