Mongo和
Java大师.我们的团队决定使用最近在MongoDB中引入的全文搜索API.但是,我们发现使用Java MongoDB驱动程序执行命令有些困难.
这是我正在使用的代码:
public BasicDBObject find(String search) { BasicDBObject searchCommand = new BasicDBObject(); searchCommand.put("text",new BasicDBObject().append("search",search)); CommandResult commandResult = db.command(searchCommand); }
这是我打印时得到的
System.out.println(commandResult) { "serverUsed" : "/127.0.0.1:27017","errmsg" : "exception: wrong type for field (text) 3 != 2","code" : 13111,"ok" : 0.0 }
解决方法
摘自Google群组(
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);
准确显示如何格式化命令.