我试图在数据库中搜索,但我没有得到任何结果,并且找不到错误,我在控制台中得到一个空数组,但数据库中充满了元素在这种情况下包括“a”。在mongod数据库中搜索
如果我试图查找数据库中的所有内容,我会看到所有结果,所以它在$ search或$ text中的内容会打破脚本。
所有的帮助都非常感谢,我一直在坐着,撕裂我的头发几天!
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
var db;
MongoClient.connect("mongodb://localhost:27017/insights", function(err, database) {
db = database;
db.collection("textstore", {}, function(err, coll) {
if (err != null) {
db.createCollection("textstore", function(err, result) {
assert.equal(null, err);
});
}
db.ensureIndex("textstore", {
document: "text"
}, function(err, indexname) {
assert.equal(null, err);
});
});
});
app.post("/search", function(req, res) {
db.collection('textstore').find({"$text": {"$search": 'a'}}).toArray(function(err, items) {
console.log(items)
})
});