Arrays 在MongoDB中,如何在文本和数组中搜索
我们有这样的db:Arrays 在MongoDB中,如何在文本和数组中搜索,arrays,mongodb,search,Arrays,Mongodb,Search,我们有这样的db: { tags : ["key1", "key2", "key3", "car", "etc.."], question : "some text here " }, { tags : ["bla1", "bla2", "bla3", "etc.."], question : "My car is here" } 我需要查询,这将得到两行关键字“车” 我试过: db.some.find( '$or' : [ ["tags" : "car" ], [ "$text" : [
{
tags : ["key1", "key2", "key3", "car", "etc.."],
question : "some text here "
},
{
tags : ["bla1", "bla2", "bla3", "etc.."],
question : "My car is here"
}
我需要查询,这将得到两行关键字“车”
我试过:
db.some.find(
'$or' : [ ["tags" : "car" ], [ "$text" : [ "$search" : "car" ] ] ]
);
“问题”是文本索引,除非您需要使用
$text
,否则可以使用纯$regex
:
db.some.find({ $or:[ {tags:"car"}, {question:/car/} ] })
我认为您有语法问题(如果您是从
mongo
shell运行此程序)。试试这个:db.some.find({'$or':[{“tags”:“car”},{“$text”:{“$search”:“car”}]})代码>