[英]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) at1.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.