您可以选择使用聚合框架:
DEV:history:PRI > db.test.find()
{ "_id" : ObjectId("51c37c0c20d107378f9af3cc"), "title" : "Some awesome title", "keywords" : [ "some", "awesome", "title" ] }
{ "_id" : ObjectId("51c37de420d107378f9af3ce"), "title" : "Some awesome title", "keywords" : [ "some", "awesome", "something" ] }
{ "_id" : ObjectId("51c37f1920d107378f9af3cf"), "title" : "Some awesome title", "keywords" : [ "something", "awesome", "someone" ] }
DEV:history:PRI > db.test.aggregate({$match : {keywords : /^som/}}, {$project:{keywords:1, _id : 0}}, {$unwind : "$keywords"}, {$match : {keywords : /^som/}}, {$group: {_id : '$keywords', count : {$sum : 1}}})
{
"result" : [
{
"_id" : "someone",
"count" : 1
},
{
"_id" : "something",
"count" : 2
},
{
"_id" : "some",
"count" : 2
}
],
"ok" : 1
}
注意:不包括多键索引,并且已经提交了一些 JIRA 问题。因此,尽管使用了索引,但并未涵盖查询(对于多键索引)
我们可以选择使用“计数”参数来决定显示自动完成的顺序。如果您不需要,请从查询中删除。