简体   繁体   English

在mongodb上按对象的子集合项属性查询

[英]Query by object's child collection item property on mongodb

I am storing a collection of the following:我正在存储以下内容的集合:

{
    "_id" : ObjectId("59ffdb0c70a6560df428aaa3"),
    "name" : "Contact Information",
    "description" : "Basic contact information about myself",
    "questions" : [ 
        {
            "caption" : "First Name",
            "value" : ""
        }, 
        {
            "caption" : "Last Name",
            "value" : ""
        }, 
        {
            "caption" : "Email",
            "value" : ""
        }, 
        {
            "caption" : "Date of bith (mm/dd/yyyy)",
            "value" : ""
        }, 
        {
            "caption" : "Street Address",
            "value" : ""
        }, 
        {
            "caption" : "Address Line 2",
            "value" : ""
        }, 
        {
            "caption" : "State",
            "value" : ""
        }, 
        {
            "caption" : null,
            "value" : null
        }, 
        {
            "caption" : "Postal Code",
            "value" : ""
        }, 
        {
            "caption" : "Country",
            "value" : ""
        }
    ]

I need to query based on the parent property "name" or by questions collection and property "caption"我需要根据父属性“名称”或问题集合和属性“标题”进行查询

So I have constructed the following query:所以我构建了以下查询:

var result =(from Query in this.CoreService.QuestionCategoriesCollection().AsQueryable()
                            from Questions in Query.questions
                            where Questions.caption.ToLower().Contains(filter.ToLower())
                            select Query).ToList();

However, when the query runs I receive the following exception:但是,当查询运行时,我收到以下异常:

Exception has occurred: CLR/System.NotSupportedException An exception of type 'System.NotSupportedException' occurred in MongoDB.Driver.dll but was not handled in user code: '$project or $group does not support {document}.'发生异常:CLR/System.NotSupportedException MongoDB.Driver.dll 中发生类型为“System.NotSupportedException”的异常,但未在用户代码中处理:“$project 或 $group 不支持 {document}。” at MongoDB.Driver.Linq.Translators.AggregateLanguageTranslator.TranslateValue(Expression node) at MongoDB.Driver.Linq.Translators.AggregateLanguageTranslator.TranslateMapping(ProjectionMapping mapping) at MongoDB.Driver.Linq.Translators.QueryableTranslator.TranslateProjectValue(Expression selector) at MongoDB.Driver.Linq.Translators.QueryableTranslator.TranslateSelectMany(SelectManyExpression node) at MongoDB.Driver.Linq.Translators.QueryableTranslator.TranslateWhere(WhereExpression node) at MongoDB.Driver.Linq.Translators.QueryableTranslator.TranslateSelect(SelectExpression node) at MongoDB.Driver.Linq.Translators.QueryableTranslator.TranslatePipeline(PipelineExpression node) at MongoDB.Driver.Linq.Translators.QueryableTranslator.Translate(Expression node, IBsonSerializerRegistry serializerRegistry, ExpressionTranslationOptions translationOptions) at MongoDB.Driver.Linq.MongoQueryProviderImpl 1.GetExecutionModel(Expression expression) at MongoDB.Driver.Linq.MongoQueryableImpl 2.ToCurs在 MongoDB.Driver.Linq.Translators.AggregateLanguageTranslator.TranslateValue(Expression node) 在 MongoDB.Driver.Linq.Translators.AggregateLanguageTranslator.TranslateMapping(ProjectionMapping mapping) 在 MongoDB.Driver.Linq.Translators.QueryableTranslator.TranslateProjectValue(Expression selector) 在.Driver.Linq.Translators.QueryableTranslator.TranslateSelectMany(SelectManyExpression node) at MongoDB.Driver.Linq.Translators.QueryableTranslator.TranslateWhere(WhereExpression node) at MongoDB.Driver.Linq.Translators.QueryableTranslator.TranslateSelect(SelectExpression node) at MongoDB.Driver .Linq.Translators.QueryableTranslator.TranslatePipeline(PipelineExpression node) at MongoDB.Driver.Linq.Translators.QueryableTranslator.Translate(Expression node, IBsonSerializerRegistry serializerRegistry, ExpressionTranslationOptions translationOptions) at 1.GetExecutionModel(Expression expression) at MongoDB.Driver.Linq.MongoQueryableImpl 2.ToCurs or(CancellationToken cancellationToken) at MongoDB.Driver.IAsyncCursorSourceExtensions.ToList[TDocument](IAsyncCursorSource`1 source, CancellationToken cancellationToken) at projectname.Services.Questions.GetQuestions(String filter) in c:\\developerment\\project\\project\\Service\\Questions.cs:line 24 at projectnameAPI.Controllers.QuestionsController.Get(String filter) in c:\\developerment\\project\\project\\Controllers\\QuestionsController.cs:line 31 at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__12.MoveNext()或 (CancellationToken cancelationToken) at MongoDB.Driver.IAsyncCursorSourceExtensions.ToList[TDocument](IAsyncCursorSource`1 source, CancellationToken cancelationToken) at projectname.Services.Questions.GetQuestions(String filter) in c:\\developerment\\project\\project\\Service\\Questions .cs:line 24 at projectnameAPI.Controllers.QuestionsController.Get(String filter) in c:\\developerment\\project\\project\\Controllers\\QuestionsController.cs:line 31 at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object [] 参数)在 Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__12.MoveNext()

I don't know what other way to write the query using linq.我不知道还有什么其他方法可以使用 linq 编写查询。

I ended up rewriting my query to this.我最终将我的查询改写为这个。

    var result =this.CoreService.QuestionCategoriesCollection().Find(c=>c.name.ToLower().Contains(filter.ToLower())
    || c.questions.Any(q=>q.caption.ToLower().Contains(filter.ToLower())));

A cleaner - but more raw - way.一种更清洁 - 但更原始 - 的方式。 Given the following collection schema example:给定以下集合模式示例:

public class Parent
{
    public IEnumerable<Child> Children { get; set; }
}

public class Child
{
    public string Id { get; set; }
}

Get the parent by child id:通过子ID获取父级:

var dictionary = new Dictionary<string, object>
{
    "Children.Id" : "childId"
}
var data = this.ParentCollection().Find(new MongoDB.Driver.CommandDocument(dictionary));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Linq查询变量对象集合的Where条件 - Linq query for Where condition on Collection of variant object's property Linq如何根据父项目的属性查询项目列表以获取子集合的组合列表 - Linq how to query a list of items for a combined list of a child collection based on a property of the parent item PetaPoco / NPoco-使用(子级)集合属性保存对象 - PetaPoco/NPoco - saving object with (child) collection property 自动映射器忽略集合对象中的子属性 - Automapper ignore child property in a collection object NHibernate Query帮助,查询子集合的子值 - NHibernate Query help, query a child collection's child value 当子对象不是列表或集合时,包括子对象和子对象 - Includes child and child's objects when the child object is not list or collection MongoDB LINQ 加入集合属性类型的属性 - MongoDB LINQ joining on collection property type's property C#从父集合的项目获取子属性 - C# Get child property from the item of Parent Collection 根据对象名称将对象属性数据绑定到收集项 - Object property databinding to a collection item based on a Name of an object 如何使用LINQ To SQL查询填充对象的子级List属性 - How can I populate an object's child List property with a LINQ To SQL query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM