[英]MongoDB collection create index not working
I want to implement a simple search engine on my application and here's the api I made:我想在我的应用程序上实现一个简单的搜索引擎,这是我制作的 api:
global.db.collection("pages").createIndex({"datas.content":"text"});
global.db.collection("pages").find({$text: {$search: req.body.query}}, {score: {$meta: "textScore"}}).sort({score:{$meta:"textScore"}}).toArray(function(err, result) {
res.json(result);
});
And here's my document structure:这是我的文档结构:
{
"_id" : ObjectId("5cd176aee5b87c095cfa30ba"),
"code" : "af34e870-70c1-11e9-9ba8-eb8f4152101a",
"datas" : {
"content" : "Donec ante sapien, gravida id risus eget,"
},
"tags" : null
}
But when I call the api
I always get null
as a result, am I doing something wrong?但是当我调用
api
时,我总是得到null
,我做错了什么吗?
Thanks谢谢
Your code has no problem forFull-Text Search in MongoDB您的代码在 MongoDB 中的全文搜索没有问题
But what's left is doubtful is the information coming to API.但剩下的值得怀疑的是来自 API 的信息。 Hope your request is
POST
and inside it a key with the name of query
.希望您的请求是
POST
并在其中包含一个名为query
的键。 But if your request is a GET
, you should use {$search: req.query}}
and you should check the data that coming from client side is ok!但是如果您的请求是
GET
,您应该使用{$search: req.query}}
并且您应该检查来自客户端的数据是否正常!
OR in res.json(result)
result is not JSON
.或
res.json(result)
结果不是JSON
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.