Skip to content

Instantly share code, notes, and snippets.

@johnlpage
Created February 7, 2022 16:05
Show Gist options
  • Save johnlpage/df4233ca127a435663a74301522df0d5 to your computer and use it in GitHub Desktop.
Save johnlpage/df4233ca127a435663a74301522df0d5 to your computer and use it in GitHub Desktop.
//Convert fields to a string of digits and dots
var s={$reduce: {input: ["$x","$y","$s"],initialValue:"",in: {$concat:["$$value",{$toString:"$$this"},"."]}}}
var mx = {$pow:[227,{$mod:[{$add:["$$this",1]},6]}]}
//Iterate it doing iv=56781234 ; iv = (iv + 227^(i%6)*(s[i]+1))%1048576
//20 bit hash - can increase
var c={$reduce: {input: {$range : [0,{$strLenCP:"$$s"}]},initialValue:56781234,
in: {$mod: [{$add:["$$value",{$multiply:[mx,{$add:[1,{$convert:{to:"int",onError:10,
input:{$substrCP:["$$s","$$this",1]}}}]}]}]},1048576]}}}
var cksum = { $let : { vars: { s: s}, in:c }}
var validator = { $expr : { $eq : [ "$cksum", cksum]},$jsonSchema: { required: ["x","y","s","cksum"]}}
db.readonlyfields.drop()
db.createCollection("readonlyfields",{ validator : validator})
{ok: 1.0}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment