0

I have

String  toInsert = "somestring";

insert(toInsert);

and

insert(String toInsert) {

     DBObject dbo = new BasicDBObject();

     dbo.put("value" : toInsert);
}

In Mongo, this is saved as

{"value" : "somestring"}

But I would like it to be saved as

{"value" : somestring}

How can I change this to be saved it Mongo like this?

The 'somestring' I am trying to insert is a Javascript Function.

2 Answers 2

1

You can create a CodeWScope object by passing the string that contains the javascript method to the constructor. Also see this question for an example on how to do it.

Sign up to request clarification or add additional context in comments.

Comments

0

You can just store the function name as a string, then when you want to actually run the function you can use reflection to load the method and run it.

See: How do I invoke a Java method when given the method name as a string? for how.

Edit: I may have misinterpreted the question, did you mean that "somestring" in your example is the name of a Java function, or did you mean that it is an actual Javascript function? If the latter is the case, then there's no reason not to store it as a string, and more context is needed to understand the problem.

1 Comment

Thanks for the pointer. I was trying to do the latter - inserting a javascript function

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.