问题描述
我有一个收藏:
{
_id - ObjectId
name - string
location - string
..
parentId - ObjectId //link to parent object
}
我有一个过滤器查询,即使使用 int 值也能很好地工作(在 C# 中):
I have a filter query and it works nice even with int values (in C#):
var q = Query.Matches(column, new BsonRegularExpression(string.Format("/^{0}/i", name)));
我应该怎么做才能按父对象的名称进行搜索?当然,使用LINQ是没有问题的,但是可以在服务器端进行搜索吗?
What should I do in order to search by the name of parent object? Of cause, using LINQ, there is no problem, but is it possible to conduct searching on the server side?
推荐答案
在 mongodb 中无法通过引用的对象字段进行搜索.总的来说,monogdb 不是关系数据库,所以在我看来它根本不应该支持任何关系.
There is not way to search by referenced object fields in mongodb. And in general monogdb is not relational database, so in my opinion it should not support any relations at all.
有一种众所周知的方法,它是创建附加字段并存储(非规范化)有关您要搜索的内容的信息.
There is good known approach it is create additional field and store (denormalize) information on what you want search.
因此,按如下方式更改架构以搜索父对象名称:
So, change your schema as follow to search on parent object name:
{
_id - ObjectId
name - string
location - string
..
parentId - ObjectId //link to parent object,
parentName
}
你应该明白的主要思想:mongodb 不是实体的,这里没有连接,没有关系.
The main idea you should understand: mongodb is not realtional, no joins here, no relations.
这篇关于使用自引用对象在集合中搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!