I was wondering if was possible to utilize functions inside of MongoDB query by including a function inside of options block or something.
Suppose my database collection has a document:
{"year": "2320"}
And I have function to format this document's year-field:
const reformat = function(input){
return input.year.substring(2,4)+input.year.substring(0,2)
}
And I'll be calling a fetch function like this:
const test = {"year": "2023"}
fetchSomething(test, {}, reformat)
My fetch function looks like this:
async function fetchSomething(query, projection, options){
const dat = await mongoCli.db("testDB").collection("testCollection").findOne(query, projection, options);
return dat;
}
So, reformat-function should modify the database's document to match year-attribute of test-variable, not the other way around.
I'm not sure if MongoDB even supports it. Looking at the MongoDB documentation...
{"year": "2320"}in the collection and your query is for{"year": "2023"}, 2) you are passing blank {} in projection 3) you are passing that function in options. ---- find query will not support external function in project, and you can not use internal property in that function, you can use operators for substing in projection and that will solve your problem if you explain your actual problem in your question.$lookup. You can continue your logic inside the sub-pipeline. In addition, there is $function in MongoDB. However, it is not recommended to use that if there are MongoDB alternatives since there will be a performance impact.