【问题标题】:Node.js/Discord.js/Mongodb Need help pulling information using keywordsNode.js/Discord.js/Mongodb 需要帮助使用关键字提取信息
【发布时间】:2020-09-06 01:18:37
【问题描述】:

我正在尝试从 mongodb 中提取信息并发送到不和谐的聊天中。现在,我正在尝试从数据库中提取信息,然后发送到控制台,然后关闭程序。我似乎无法弄清楚如何从所有文档中提取信息。它需要在所有文档中搜索特定关键字。这是我的代码:

const database = 'pidpid';

MongoClient.connect(url, function(err, user){
    assert.equal(null, err);
    console.log("Connected successfully to server");

    var db = user.db(database);
    db.collection("ingest").findOne({}, function(err,result){
        if(err) throw err;
        console.log(result.url);
        user.close();
    });
});

【问题讨论】:

    标签: node.js mongodb discord.js


    【解决方案1】:

    首先,使用您要使用的集合上的字段创建一个索引。

    https://docs.mongodb.com/manual/reference/method/db.collection.createIndex/

    接下来,您将能够使用 $text 后跟 $search

    .findOne({$text:{$search: <regex or text here>}}
    

    https://docs.mongodb.com/manual/reference/operator/query/text/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-04
      • 2020-11-30
      • 2011-09-07
      • 1970-01-01
      • 1970-01-01
      • 2017-03-13
      • 2021-04-15
      • 2019-09-20
      相关资源
      最近更新 更多