2

How to use MongoDB runCommand method?

I am using db.col.runCommand("text",{search:"searchword"}) to do full text search from MongoDB console. But I want to use this command in Java, can any one suggest me how to use this in Java?

1 Answer 1

4

You can try this:

DBObject searchCmd = new BasicDBObject();
searchCmd.put("text", collection); // the name of the collection (string)
searchCmd.put("search", query); // the term to search for (string)
CommandResult commandResult = db.command(searchCmd);
Sign up to request clarification or add additional context in comments.

6 Comments

i'm getting this error "can't serialize class com.mongodb.DBApiLayer$MyCollection" for above code
can you post some code? For which line? Do you pass the collection name as a string or as some kind of other object?
i'm getting error at this line CommandResult commandResult = db.command(searchCmd);
My code....DBCollection col = db.getCollection("poc");DBObject searchCmd = new BasicDBObject(); searchCmd.put("text", col); searchCmd.put("search", "searchword"); CommandResult commandResult = db.command(searchCmd); System.out.println(commandResult);
Thanks, i have changed my code its working now but i have created text indexes for two fields. so my code is showing this err msg "errmsg" : "too many text index for: soft.poc, how to do full text search on two indexed fields, please suggest me
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.