Das folgende Stück Code speichern:Wie kann ich eine gespeicherte Javascript in mongodb von node.js
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);
});
});
});
});
Ausgänge:
[Error: eval failed: invoke failed: JS Error: TypeError: myFunction is not a function nofile_a:0]
ich, dass das Problem auf die Anführungszeichen verwandt ist herausgefunden ("") Wrapping der Funktionsdefinition.
Auf Mongo-cli:
> db.system.js.find()
{ "_id" : "myFunction", "value" : "function myFunction() { return 123; }" }
Aber nach:
> db.system.js.save({_id : "myFunction", value : function myFunction() { return 123; }});
und
> db.system.js.find()
{ "_id" : "myFunction", "value" : function cf__5__f_myFunction() {
return 123;
} }
die db.eval ("myFunction()"), funktioniert!
Also meine Frage ist: Wie kann ich eine gespeicherte Prozedur von node.js mit dem node-mongodb-native Treiber speichern?