【问题标题】:MongoDB Full Text Search result in " Bad Command" errorMongoDB 全文搜索导致“错误命令”错误
【发布时间】:2016-04-26 20:50:46
【问题描述】:

我正在尝试在 Mongo DB 3.2 Java 驱动程序中进行全文搜索

以下是我的代码:

  DBObject textSearchCommand = new BasicDBObject();
    textSearchCommand.put("text", collectionName);
    textSearchCommand.put("search", "MAURICE");
CommandResult commandResult = db.command(textSearchCommand);
     System.out.println("Command result is "+commandResult.toString());

出现以下错误:

命令结果是 { "ok" : 0.0 , "errmsg" : "no such command: 'text', bad cmd: '{ text: \"citizen5\", search: \"MAURICE\" }'" , “代码”:59}

我已经创建了 textIndex :

{ "v" : 1 , "key" : { "_fts" : "text" , "_ftsx" : 1} , "name" : "MyTextIndex" , "ns" : "matcher.citizen5" , "weights " : { "address" : 1 , "firstname" : 1 , "lastname" : 1 , "metaaddress" : 1 , "metafirstname" : 1 , "metalastname" : 1 , "mobile" : 1} , "default_language" : "english" , "language_override" : "language" , "textIndexVersion" : 3}

谁能指出我哪里做错了?

【问题讨论】:

    标签: mongodb


    【解决方案1】:

    我得到了解决方案。 MongoDB 3.0 及更高版本的文本命令已禁用。

    因此,您可以使用普通的 find 命令通过查询进行搜索。

    这是我的代码:

     BasicDBObject query = new BasicDBObject();
      query.put("$text", new BasicDBObject("$search",value));
      DBCursor cursor = db.getCollection(dbName).find(query);
      while (cursor.hasNext()) {
          BasicDBObject obj = (BasicDBObject) cursor.next();
          System.out.println(obj.toString()); 
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-26
      • 2016-03-03
      • 2015-03-07
      • 2021-09-25
      • 2014-01-11
      • 2013-05-28
      相关资源
      最近更新 更多