How to use Java driver to execute full-text search commands in mongodb?
•
Java
Mongo and Java masters Our team decided to use the full-text search API recently introduced in mongodb However, we find it difficult to execute commands using the Java mongodb driver
This is the code I'm using:
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 got when I printed it
System.out.println(commandResult) { "serverUsed" : "/127.0.0.1:27017","errmsg" : "exception: wrong type for field (text) 3 != 2","code" : 13111,"ok" : 0.0 }
Solution
From Google Group( https://groups.google.com/forum/?fromgroups# ! Post on topic / mongodb user / 7jwubunucfq):
final DBObject textSearchCommand = new BasicDBObject(); textSearchCommand.put("text",collectionName); textSearchCommand.put("search",textToSearchFor); final CommandResult commandResult = db.command(textSearchCommand);
Shows exactly how to format commands
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码