JavaScript 함수는 system.js라는 시스템 컬렉션을 사용하여 재사용을 위해 저장할 수 있습니다. 함수를 저장하려면 db.collection.save(),
를 사용하세요.먼저 함수를 생성해 보겠습니다. 다음은 쿼리입니다 -
> db.system.js.save({
... _id: "displayMessage",
... value: function (data) {
... return 'The Name is: ' + data;
... }
... }) 이것은 다음과 같은 출력을 생성합니다 -
WriteResult({
"nMatched" : 0,
"nUpserted" : 1,
"nModified" : 0,
"_id" : "displayMessage"
}) 다음은 저장 함수를 실행하는 쿼리입니다 -
> db.eval("displayMessage('John')")
WARNING: db.eval is deprecated 이것은 다음과 같은 출력을 생성합니다 -
The Name is: John