trying to write a Mongo query that will Base64 Decode a field that is Base64 encoded and then perform a simple "like" on the decoded value. I'm following a couple of different posts as well as the Mongo docs, but can't seem to get the syntax correct. I basically want to do a query like this :
db.getCollection('my-collection').find (
{ base64Decode(edmDocumentId): /ni-billing-retro/ }
);
Where base64Decode() is a custom function inserted into system.js.
Posts:
----------------
Export text stored as Bindata in mongodb
How to query MongoDB with "like"?
What I've done so far :
- I saved the base64Decode() function to the system.js, and I can see the function....https://docs.mongodb.com/manual/tutorial/store-javascript-function-on-server/.
db.system.js.insertOne( {
_id: "base64Decode",
value : function (s) {
var e={},i,k,v=[],r='',w=String.fromCharCode,u=0;
var n=[[65,91],[97,123],[48,58],[43,44],[47,48]];
for(z in n){for(i=n[z][0];i<n[z][1];i++){v.push(w(i));}}
for(i=0;i<64;i++){e[v[i]]=i;}
function a(c){
if(c<128)r+=w(c);else if(c>=192)u=c;else r+=w(((u&31)<<6)+(c&63));
}
for(i=0;i<s.length;i+=72){
var b=0,c,x,l=0,o=s.substring(i,i+72);
for(x=0;x<o.length;x++){
c=e[o.charAt(x)];b=(b<<6)+c;l+=6;
while(l>=8)a((b>>>(l-=8))%256);
}
}
return r;
}
});
- I've tried using $where, to no avail...returns ReferenceError: edmDocumentId is not. Added the db.loadServerScripts(); to fix the base64Decode() Reference error.
db.loadServerScripts();
db.getCollection('rapid-document-meta').find (
{ $where: (base64Decode(edmDocumentId) == /ni-billing/) }
);
- I've tried doing a straight find (), Unexpected token : Line 2
db.getCollection('rapid-document-meta').find (
{ base64Decode(edmDocumentId): /ni-billing-retro/ }
);
- Tried the following from Calling db.system.js Function in $where : ReferenceError: edDocumentId is not defined, even though edmDocumentId is on every single record.
db.loadServerScripts();
db.getCollection('rapid-document-meta').mapReduce (
base64Decode(edDocumentId),
function() {},
{ "out": { "inline": 1 } }
);
Does someone have an example of a find query that uses a custom function from system.js??? Mongo version 4.0.8.