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.

je4d
  • 7,060
  • 29
  • 44
Skynet
  • 607
  • 2
  • 9
  • 25

2 Answers2

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.

Community
  • 1
  • 1
Ren
  • 668
  • 4
  • 6
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.

Community
  • 1
  • 1
jli
  • 6,274
  • 2
  • 25
  • 37