我试图在我的colloection中使用聚合框架搜索“credit”这个词,但是我有一个错误。任何人都可以帮助弄清问题是什么。
db.complaints.createIndex({issue:1})
db.complaints.aggregate([
{$match:{$text:{$search:"credit"}}},
{$project:{issue:1,_id:0}}
])
我遇到了这个错误:
assert: command failed: {
"errmsg" : "exception: error processing query: ns=customers.complaints l
imit=0 skip=0\nTree: TEXT : query=credit, language=, tag=NULL\nSort: {}\nProj: {
$textScore: { $meta: \"textScore\" }, issue: 1, _id: 0 }\n planner returned err
or: need exactly one text index for $text query",
"code" : 2,
"ok" : 0
} : aggregate failed
Error: command failed: {
"errmsg" : "exception: error processing query: ns=customers.complaints l
imit=0 skip=0\nTree: TEXT : query=credit, language=, tag=NULL\nSort: {}\nProj: {
$textScore: { $meta: \"textScore\" }, issue: 1, _id: 0 }\n planner returned err
or: need exactly one text index for $text query",
"code" : 2,
"ok" : 0
} : aggregate failed
at Error (<anonymous>)
at doassert (src/mongo/shell/assert.js:11:14)
at Function.assert.commandWorked (src/mongo/shell/assert.js:254:5)
at DBCollection.aggregate (src/mongo/shell/collection.js:1278:12)
at (shell):1:15
2015-09-01T17:10:45.512+0100 E QUERY Error: command failed: {
"errmsg" : "exception: error processing query: ns=customers.complaints l
imit=0 skip=0\nTree: TEXT : query=credit, language=, tag=NULL\nSort: {}\nProj: {
$textScore: { $meta: \"textScore\" }, issue: 1, _id: 0 }\n planner returned err
or: need exactly one text index for $text query",
"code" : 2,
"ok" : 0
} : aggregate failed
at Error (<anonymous>)
at doassert (src/mongo/shell/assert.js:11:14)
at Function.assert.commandWorked (src/mongo/shell/assert.js:254:5)
at DBCollection.aggregate (src/mongo/shell/collection.js:1278:12)
at (shell):1:15 at src/mongo/shell/assert.js:13
答案 0 :(得分:0)
如果你看一下你的错误信息,你会看到以下消息:只需要一个文本索引用于$ text查询。
您的帖子显示您确实在issue
上创建了索引。但是,您似乎没有为您的收藏集声明Text Index。
如果您希望issue
文档字段可以搜索文本,请按如下方式创建文本索引:
db.complaints.createIndex({issue:"text"})
(注意,每个集合可能只有一个文本索引。但是,文本索引中包含的字段是在创建索引时建立的。再次参见上面提到的文本索引链接。 )
答案 1 :(得分:0)
此查询将起作用:
db.things.aggregate([{$match:{issue:"credit"}},{$project:{issue:1,_id:0}}])