因此,您的 json 键的值是序列化的 json。如果您正在搜索具有包含单词“结果”的 json 值的文档,则可以使用 $regex 运算符。您没有说明您拥有多少条记录或您期望的性能,因此 Solr / Lucene 可能是矫枉过正。您还可以查看最新 MongoDB 版本中的新自由文本搜索功能。
尝试:
db.test.find({json:/result:/i});
在我的机器上:
> db.test.insert({ s_id:0, c_id:1, json:"{result:[104192,42068],id:1}" })
> db.test.find({json:/result:/i});
{ "_id" : ObjectId("511d21fe0ff85d1b95a5f8b1"), "s_id" : 0, "c_id" : 1, "json" : "{result:[104192,42068],id:1}" }
顺便说一句,来自 MongoDB 手册:
“$regex 只有在正则表达式在字符串的开头(即 ^)有一个锚点并且是区分大小写的匹配时才能有效地使用索引”
所以你实际上可能想要
> db.test.ensureIndex({json:1})
> db.test.find({json:{ $regex:'^{result:'}}).explain();
{
"cursor" : "BtreeCursor json_1 multi",
"isMultiKey" : false,
"n" : 1,
"nscannedObjects" : 1,
"nscanned" : 1,
"nscannedObjectsAllPlans" : 1,
"nscannedAllPlans" : 1,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 0,
"indexBounds" : {
"json" : [
[
"",
{
}
],
[
/^{result:/,
/^{result:/
]
]
},
"server" : "Micks-MacBook-Pro-3.local:27017"
}