Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-2629

Executing function in mongo from java

    • Type: Icon: Task Task
    • Resolution: Works as Designed
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: 3.3.0
    • Component/s: BSON
    • Labels:
      None

      I have run into what seems to be a bug but I have a feeling I am not using something correctly since the behavior is a little strange.

      Goal:
      I am trying to set up Mongo to auto increment IDs for me so that when I store documents into the database they are given a unique, incrementing id based on the collection they are in.

      On app startup my program, through the java driver, inserts the following documents:

      db.system.js

      {
      		_id : "incID" ,
      		value : function (x) {
      			var num = db.incrementers.findOne({_id: x}).value;
      			db.incrementers.updateOne({_id: x}, { "$inc": { value: 1}});
      			return num;
      		}
      }
      
      

      db.incrementers

      [
        { _id: "test", value: 1}
      ]
      
      

      the function takes in the id of the incrementer, adds one and returns the value so the document can be indexed. This all works fine.

      The problem comes when I try to insert a document that uses this function.
      in the mongo shell if i insert the document:

      { testID: incID('test') }
      

      the function is called and the result is correctly stored into the document. However, if I insert the same document into mongo using the java driver:

      new Document( "testID", new BsonJavaScript("incID('test')"); 
      

      the function call itself is stored and not the result of the function.

      While I have a feeling that the intended purpose of the BsonJavaScript object is to store the js code itself (and not run it), how am i supposed to run the code on insert?

            Assignee:
            Unassigned Unassigned
            Reporter:
            MacKenzie Aaron [X]
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: