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

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

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

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

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

  1. const { MongoClient } = require('mongodb');
  2. async function main() {
  3. const uri = "***************************";
  4. const client = new MongoClient(uri);
  5. const mydb = client.db("Cluster0").collection("-100****");
  6. try {
  7. await client.connect();
  8. await mydb.createIndex({ file_name: "text" });
  9. } finally {
  10. await client.close();
  11. }
  12. }
  13. main().catch(console.error);

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

  1. const { MongoClient } = require('mongodb');
  2. async function main() {
  3. const uri = "***************************";
  4. const mydb = client.db("Cluster0").collection("-100****");
  5. try {
  6. await client.connect();
  7. await myFunc(mydb,"Ghilli");
  8. } finally {
  9. await client.close();
  10. }
  11. }
  12. main().catch(console.error);
  13. async function myFunc(mydb,searchWord) {
  14. const result = await mydb.find({ $text: { $search: searchWord } });
  15. if (result) {
  16. console.log(result);
  17. } else {
  18. console.log(`No listings found with the name`);
  19. }
  20. }

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

暂无答案!

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

相关问题