mongo 中多个 $text 搜索的 $and 运算符
$and operator on multiple $text search in mongo
是否可以在 mongo 中对多个 $text 索引搜索使用 $and 运算符?
我的数据库的 tp 集合中有文档
> db.tp.find()
{ "_id" : ObjectId("...."), "name" : "tp", "dict" : { "item1" : "random", "item2" : "some" } }
{ "_id" : ObjectId("...."), "name" : "tp", "dict" : { "item3" : "rom", "item4" : "tttt" } }
那我做
> db.tp.createIndex({ "$**": "text" })
> db.tp.find({ $and: [{$text : { $search: "random" } }, {$text : { $search: "redruth" } }]})
它失败了
Error: error: {
"waitedMS" : NumberLong(0),
"ok" : 0,
"errmsg" : "Too many text expressions",
"code" : 2
}
但是文本索引搜索适用于单次搜索,所以是否不能使用 $and 运算符绑定多个文本搜索?顺便说一下,我使用通配符 $**
进行索引,因为我想搜索整个文档。
一个查询最多可以指定一个 $text
表达式。参见:
https://docs.mongodb.com/manual/reference/operator/query/text/
基于mongoDB文档,AND运算符可以通过组合引号和space直接在搜索词中使用。例如,我们搜索 "ssl certificate" AND "authority key",那么查询应该是:
> db.tp.find({'$text': {'$search': '"ssl certificate" "authority key"'}})
是否可以在 mongo 中对多个 $text 索引搜索使用 $and 运算符?
我的数据库的 tp 集合中有文档
> db.tp.find()
{ "_id" : ObjectId("...."), "name" : "tp", "dict" : { "item1" : "random", "item2" : "some" } }
{ "_id" : ObjectId("...."), "name" : "tp", "dict" : { "item3" : "rom", "item4" : "tttt" } }
那我做
> db.tp.createIndex({ "$**": "text" })
> db.tp.find({ $and: [{$text : { $search: "random" } }, {$text : { $search: "redruth" } }]})
它失败了
Error: error: {
"waitedMS" : NumberLong(0),
"ok" : 0,
"errmsg" : "Too many text expressions",
"code" : 2
}
但是文本索引搜索适用于单次搜索,所以是否不能使用 $and 运算符绑定多个文本搜索?顺便说一下,我使用通配符 $**
进行索引,因为我想搜索整个文档。
一个查询最多可以指定一个 $text
表达式。参见:
https://docs.mongodb.com/manual/reference/operator/query/text/
基于mongoDB文档,AND运算符可以通过组合引号和space直接在搜索词中使用。例如,我们搜索 "ssl certificate" AND "authority key",那么查询应该是:
> db.tp.find({'$text': {'$search': '"ssl certificate" "authority key"'}})