Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何获取mongodb查询结果中的标记计数?_Mongodb_Mongodb Query - Fatal编程技术网

如何获取mongodb查询结果中的标记计数?

如何获取mongodb查询结果中的标记计数?,mongodb,mongodb-query,Mongodb,Mongodb Query,我试图在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

我试图在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" ] }
正如您所看到的,查询查找“四”或“五”,但我希望得到按“最佳匹配”排序的结果(两个匹配的标记优于一个匹配的标记)

我怎样才能做到这一点?地图缩小

提前感谢:


哈维

我真的很抱歉让你失望,但蒙戈现在无能为力。 唯一的选择是在应用程序端实现排序。我知道这可能效率太低,但没有其他选择

我唯一能帮你的就是建议你换一个

$or : [ { tags: 'four' }, { tags: 'five' } ]

这对查询没有帮助(我的意思是排序结果),但是如果有很多不同的标记,那么在服务器上创建查询会更容易

tags:{$in:['four','five']}