mongodb文本搜索不工作(node js)

iibxawm4  于 2021-09-23  发布在  Java
关注(0)|答案(0)|浏览(165)

我正在尝试使用NodeJS创建电报机器人。但我有一个大问题。
这里是我的数据库上的一个示例数据。。。

_id:"******",
channel_id:-100******,
file_name:"Ghilli [2004]Tamil Proper HDRip.mkv",
file_size:459080491,
link:"https://t.me/c/*******/20245"

首先,我运行下面的代码。创建索引(但它没有在控制台上写入任何内容)

const { MongoClient } = require('mongodb');

async function main() {

    const uri = "***************************";

    const client = new MongoClient(uri);
    const mydb = client.db("Cluster0").collection("-100****");

    try {
        await client.connect();

        await mydb.createIndex({ file_name: "text" });

    } finally {
        await client.close();
    }
}
main().catch(console.error);

然后我运行下面的代码来搜索它。

const { MongoClient } = require('mongodb');

async function main() {
    const uri = "***************************";
    const mydb = client.db("Cluster0").collection("-100****");
    try {
        await client.connect();

        await myFunc(mydb,"Ghilli");

    } finally {
        await client.close();
    }
}

main().catch(console.error);

async function myFunc(mydb,searchWord) {

    const result = await mydb.find({ $text: { $search: searchWord } });

    if (result) {
        console.log(result);
    } else {
        console.log(`No listings found with the name`);
    }
}

但它输出了一个错误https://pastehere.xyz/iejfiywq/
请帮我解决这个问题。。。。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题