Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7833797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:54:01+00:00 2026-06-02T12:54:01+00:00

The following piece of code: var mongo = require(‘mongodb’); var db = new mongo.Db(‘test’,

  • 0

The following piece of code:

var mongo = require('mongodb');

var db = new mongo.Db('test', new mongo.Server('127.0.0.1', 27017, {}));

var callback = function (e, result) {
if (e) {
    console.log(e);
    process.exit(1);
}

console.log(result);
process.exit(0);
}

db.open(function (e) {
if (e)  callback(e);

db.collection('system.js', function (e, coll) {
    if (e)  callback(e);

    coll.save({
        "_id" : "myFunction",
        "value" : "function myFunction() { return 123; }"
    }, function (e) {
        if (e)  callback(e);

        db.eval("myFunction()", [], function (e, result) {
            if (e)  callback(e);

            callback(undefined, result);
        });

    });
});

});

outputs:

[Error: eval failed: invoke failed: JS Error: TypeError: myFunction is not a function nofile_a:0]

I found out that the problem is related to the quotes (“”) wrapping the function definition.

On mongo-cli:

> db.system.js.find()
{ "_id" : "myFunction", "value" : "function myFunction() { return 123; }" }

But after:

> db.system.js.save({_id : "myFunction", value : function myFunction() { return 123; }});

and

> db.system.js.find()
{ "_id" : "myFunction", "value" : function cf__5__f_myFunction() {  
return 123;  
} }

the db.eval(“myFunction()”), works!

So, my question is: How can I save a stored procedure from node.js using the node-mongodb-native driver?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T12:54:04+00:00Added an answer on June 2, 2026 at 12:54 pm

    After reading the documentation about the driver data types, I realized that instead of passing the string with the code, I needed to pass a mongo.Code object:

    {
      "_id" : "myFunction",
      "value" : new mongo.Code("function myFunction() { return 123; }")
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following piece of code: using(var data = new SomeDataContext(ConnectionString)) { data.ObjectTrackingEnabled =
I have the following piece of code: // setup the AJAX request var pageRequest
So I have the following piece of code: var structures = { loginStructure :
I have the following piece of code: function checkAmount() { var PayField = document.getElementById('paymentamount');
Say you have the following piece of code: function someProcess() { var deferred =
I have the following piece of code: var blah = function(x, y){ var e
Consider the following piece of code. int var; cout << (long)&var; My doubt is
I have the following piece of code to display map in my test web
The backbone.js annotated source describes the following piece of code var Backbone; if (typeof
Consider the following piece of code. <html> <body> <script> var x = 5; //globally

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.