Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom index comparator in MongoDB

I'm working with a dataset composed by probabilistic encrypted elements indistinguishable from random samples. This way, sequential encryptions of the same number results in different ciphertexts. However, these still comparable through a special function that applies algorithms like SHA256 to compare two ciphertexts.

I want to add a list of the described ciphertexts to a MongoDB database and index it using a tree-based structure (i.e.: AVL). I can't simply apply the default indexing of the database because, as described, the records must be comparable using the special function.

An example: Suppose I have a database db and a collection c composed by the following document type:

{
  "_id":ObjectId,
  "r":string
}

Moreover, let F(int,string,string) be the following function:

F(h,l,r) = ( SHA256(l | r) + h ) % 3

where the operator | is a standard concatenation function.

I want to execute the following query in an efficient way, such as in a collection with some suitable indexing:

db.c.find( { F(h,l,r) :{ $eq: 0 } } )

for h and l chosen arbitrarily but not constants. I.e.: Suppose I want to find all records that satisfy F(h1,l1,r), for some pair (h1, l1). Later, in another moment, I want to do the same but using (h2, l2) such that h1 != h2 and l1 != l2. h and l may assume any value in the set of integers.

How can I do that?

like image 354
Pedro Alves Avatar asked Jul 04 '16 13:07

Pedro Alves


1 Answers

You can execute this query use the operator $where, but this way can't use index. So, for query performance it's dependents on the size of your dataset.

db.c.find({$where: function() { return F(1, "bb", this.r) == 0; }})

Before execute the code above, you need store your function F on the mongodb server:

db.system.js.save({
    _id: "F",
    value: function(h, l, r) {
        // the body of function
    }
})

Links:

  • store javascript function on server
like image 131
Shawyeok Avatar answered Oct 31 '22 04:10

Shawyeok