How to execute a full-text search command in MongoDB using a Java driver?

Guru Mongo and Java. Our team decided to use the full-text search API recently introduced in MongoDB. However, we found some difficulties in executing the command using the Java MongoDB driver.

here is my code i use:

public BasicDBObject find(String search) { BasicDBObject searchCommand = new BasicDBObject(); searchCommand.put("text", new BasicDBObject().append("search", search)); CommandResult commandResult = db.command(searchCommand); } 

This is what I get when I print

  System.out.println(commandResult) { "serverUsed" : "/127.0.0.1:27017" , "errmsg" : "exception: wrong type for field (text) 3 != 2" , "code" : 13111 , "ok" : 0.0 } 
+6
source share
1 answer

Taken from a post in a Google group ( https://groups.google.com/forum/?fromgroups#!topic/mongodb-user/7jWUbunUcFQ ):

  final DBObject textSearchCommand = new BasicDBObject(); textSearchCommand.put("text", collectionName); textSearchCommand.put("search", textToSearchFor); final CommandResult commandResult = db.command(textSearchCommand); 

Shows how to format a command.

+11
source

All Articles