我試圖在數據庫中搜索,但我沒有得到任何結果,並且找不到錯誤,我在控制檯中得到一個空數組,但數據庫中充滿了元素在這種情況下包括「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)
})
});