[JAVA-2629] Executing function in mongo from java Created: 10/Oct/17 Updated: 27/Oct/23 Resolved: 10/Oct/17 |
|
| Status: | Closed |
| Project: | Java Driver |
| Component/s: | BSON |
| Affects Version/s: | 3.3.0 |
| Fix Version/s: | None |
| Type: | Task | Priority: | Minor - P4 |
| Reporter: | Aaron [X] | Assignee: | Unassigned |
| Resolution: | Works as Designed | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Description |
|
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: On app startup my program, through the java driver, inserts the following documents: db.system.js
db.incrementers
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.
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:
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? |
| Comments |
| Comment by Ross Lawley [ 10/Oct/17 ] |
|
Hi MacKenzie, You cannot call a javascript function from the MongoDB Java Driver. Stored javascript functions are only callable from within the shell. MongoDB does not support auto incrementing ids as that is not a scalable form of _id for a distributed database. We do have ObjectId's which is scalable and meets the need as the default _id value for documents in MongoDB. There are algorithms you can implement in the driver to achieve incrementing ids. An older version of the documentation included one such example. However, as it is costly in terms of roundtrips for little benefit it was dropped from the documentation when MongoDB 3.2 came out. So I advise caution and reflection on the motivation for the need for such a counter. There is no built in support and while you can manually work around and implement a solution, if implementing it purely for aesthetic reasons, I would recommend not doing so. Just to let you know for future reference this project is for Java driver bugs or feature requests. A better place for questions regarding MongoDB usage or the Java driver specifics is the mongodb-user mailing list or stackoverflow as you will reach a boarder audience there. If your business requires an answer from MongoDB within a set time frame then we do offer production support. I hope that helps, Ross |