我正在尝试在 mongodb 中搜索基于标签的文章:
> use test;
> db.articles.save( { name: "article1", tags: ['one', 'two', 'three'] } );
> db.articles.save( { name: "article2", tags: ['two', 'three'] } );
> db.articles.save( { name: "article3", tags: ['one', 'three'] } );
> db.articles.save( { name: "article4", tags: ['one', 'two', 'four'] } );
> db.articles.save( { name: "article5", tags: ['four', 'five'] } );
> db.articles.ensureIndex( { tags: 1 } );
> db.articles.find( { $or : [ { tags: 'four' }, { tags: 'five' } ] } )
{ "_id" : ObjectId("509d7b555bff77729a26a232"), "name" : "article4", "tags" : [ "one", "two", "four" ] }
{ "_id" : ObjectId("509d7b555bff77729a26a233"), "name" : "article5", "tags" : [ "four", "five" ] }
如您所见,查询查找“四”或“五”,但我希望按“最佳匹配”排序结果(匹配的两个标签优于匹配的一个标签)。
我怎样才能完成这项工作?地图缩小?
提前致谢:
夏比