Given a structure like { name: String, middlename: String, lastname: String }
is there a way in mongo both mongoose for node and in php to search a term in the concatenation of the fields, for example in mysql i can do something like
对于像{name: String, middlename: String, lastname: String}这样的结构,在mongo中是否有一种方法既可以搜索node又可以搜索php中的一个词,比如在mysql中,我可以做一些类似的事情
select * from persons
where concat_ws(' ',name, middlename, lastname) like '%John A%' OR
concat_ws(' ',lastname, middlename, name) like '%John A%'
group by id
this way i dont need to explicitly make inputs for every field just one but i dont see how to accomplish something like that in mongo
这样,我就不需要为每个领域都明确地提供输入,只需要输入一个字段,但我不知道如何在mongo完成这样的工作
1 个解决方案
#1
3
You can use the $where operator in the find()
query:
您可以在find()查询中使用$where操作符:
db.persons.find({$where:function(){
var search = /John A/;
var matchA = (this.name+" "+this.middlename+" "+this.lastname).match(search);
var matchB = (this.lastname+" "+this.middlename+" "+this.name).match(search);
return !!(matchA || matchB);
}})
or, you could aggregate the results.
或者,你可以聚合结果。
-
Project
two extra fields, which contain the concatenated value.(use $concat operator.) - 项目两个额外的字段,其中包含连接的值。(使用concat美元操作符)。
-
Match
using a regular expression pattern.(SQL to Mongo Mapping chart.) - 使用正则表达式模式进行匹配。(SQL to Mongo Mapping chart)
MongoDb aggregation pipeline:
MongoDb聚合管道:
var search = /John A/;
db.persons.aggregate([
{$project:{"concat_name_a":{$concat:["$name"," ","$middlename"," ","$lastname"]},
"concat_name_b":{$concat:["$lastname"," ","$middlename"," ","$name"]},
"name":1,"middlename":1,"lastname":1}},
{$match:{$or:[{"concat_name_a":search},{"concat_name_b":search}]}},
{$project:{"name":1,"middlename":1,"lastname":1}}
])
Going for either of the approaches would be fine here, since both the approaches cannot make use of any indexes on the collection.
使用这两种方法都可以,因为这两种方法都不能使用集合上的任何索引。
#1
3
You can use the $where operator in the find()
query:
您可以在find()查询中使用$where操作符:
db.persons.find({$where:function(){
var search = /John A/;
var matchA = (this.name+" "+this.middlename+" "+this.lastname).match(search);
var matchB = (this.lastname+" "+this.middlename+" "+this.name).match(search);
return !!(matchA || matchB);
}})
or, you could aggregate the results.
或者,你可以聚合结果。
-
Project
two extra fields, which contain the concatenated value.(use $concat operator.) - 项目两个额外的字段,其中包含连接的值。(使用concat美元操作符)。
-
Match
using a regular expression pattern.(SQL to Mongo Mapping chart.) - 使用正则表达式模式进行匹配。(SQL to Mongo Mapping chart)
MongoDb aggregation pipeline:
MongoDb聚合管道:
var search = /John A/;
db.persons.aggregate([
{$project:{"concat_name_a":{$concat:["$name"," ","$middlename"," ","$lastname"]},
"concat_name_b":{$concat:["$lastname"," ","$middlename"," ","$name"]},
"name":1,"middlename":1,"lastname":1}},
{$match:{$or:[{"concat_name_a":search},{"concat_name_b":search}]}},
{$project:{"name":1,"middlename":1,"lastname":1}}
])
Going for either of the approaches would be fine here, since both the approaches cannot make use of any indexes on the collection.
使用这两种方法都可以,因为这两种方法都不能使用集合上的任何索引。