5

I have few stored Javascripts in mongodb. I am using spring springframework.data.mongodb to access mongodb. How can i execute stored JavaScripts/procedures from this ?

2 Answers 2

9

found the solution.

BasicDBObject obj = new BasicDBObject();
obj.append( "$eval" , "stored_javascript_name()" );

CommandResult t=mongoTemplate.executeCommand(obj);
Object obj1=t.get("retval");
Sign up to request clarification or add additional context in comments.

2 Comments

If the store procedure has parameters?
I found that if we wanna add parameter, we can do like this: int id = 12; BasicDBObject obj = new BasicDBObject(); obj.append( "$eval" , "stored_javascript_name("+ id +")" ); CommandResult t=mongoTemplate.executeCommand(obj); Object obj1=t.get("retval");
1
BasicDBObject obj = new BasicDBObject();
String menu="湖南卫视";
String program="天路啦:东方不亮西方亮";
obj.append( "$eval" , "keyword_store('"+menu+"','"+program+"')" );

Another thing you should know is adding single quotes when you transfer string parameter.

Comments

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.