尝试编写一个 Mongo 查询,它将 Base64 解码一个 Base64 编码的字段,然后对解码后的值执行一个简单的“喜欢”。我正在关注几个不同的帖子以及 Mongo 文档,但似乎无法获得正确的语法。我基本上想做这样的查询:
db.getCollection('my-collection').find (
{ base64Decode(edmDocumentId): /ni-billing-retro/ }
);
其中 base64Decode() 是插入到 system.js 中的自定义函数。
帖子:
----------------
将文本存储为 mongodb 中的 Bindata
如何用“like”查询 MongoDB?
到目前为止我所做的:
- 我将base64Decode()函数保存到system.js中,可以看到函数.... 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;
}
});
- 我尝试使用 $where,但无济于事...返回 ReferenceError: edmDocumentId 不是。添加了 db.loadServerScripts(); 修复 base64Decode() 引用错误。
db.loadServerScripts();
db.getCollection('rapid-document-meta').find (
{ $where: (base64Decode(edmDocumentId) == /ni-billing/) }
);
- 我试过直接查找(),意外令牌:第 2 行
db.getCollection('rapid-document-meta').find (
{ base64Decode(edmDocumentId): /ni-billing-retro/ }
);
- 从Calling db.system.js Function in $where尝试以下操作:ReferenceError: edDocumentId 未定义,即使 edmDocumentId 在每条记录上。
db.loadServerScripts();
db.getCollection('rapid-document-meta').mapReduce (
base64Decode(edDocumentId),
function() {},
{ "out": { "inline": 1 } }
);
有人有使用 system.js 中的自定义函数的查找查询的示例吗???蒙戈版本 4.0.8。