How to make mathematical operation between two array on mongodb ( mongoDB ile iki dizi arasında matematiksel işlem yapma )
MongoDB veri tabanını kullanarak iki dizi arasında işlem yapmak için mapReduce işleminden yaralanılmıştır. yapılan işlem her iki dizinin elemanları bir birinden çıkartılır ([a[0]-b[0] a[1]-b[1]]) daha sonra her elemanın karesi alınır ve tüm elemanlar toplanıp kare kökü alınacaktır. Bu işlemi yapan hem mongo Shell kodunu hem de python kodunu sizlerle paylaşacağım;
in general, use MapReduce method under “Aggregation” main title for making matematical operation between two array on database of MongoDB. First step of this operation is subtract one to one between each elements of two array such as ([a[0] – b[0] a[1] – b[1]] …). Second step, it squares tehir each elements lsuch as (c[0]^2 c[1]^2 …). third step, it sums all of element squares. Finally, it takes square root of the most recent value.
mongoDB Shell Code:
var embed= [1, 2, 3, ...] #dizinizin boyutu size bagli var result = db.faceEmbedding.mapReduce( function() { var inpu = KEYS; //this.embed var value = Array.sum(this.encodings.map(function(el,idx) { var mul = Math.abs(el - inpu[idx]); return mul * mul; })); value = Math.sqrt(value); emit(this._id,{"name":this.name, "score": value }); }, function(key,values) { var output = []; values.forEach(function(value) { value.output.forEach(function(item) { output.push(item); }); }); return { "output": output}; }, { "out": "Output", scope: {KEYS: embed}} ); result.find({"value.score": {"$lte": 0.6}}).sort({"value.score": -1})
python Code:
from pymongo import MongoClient from bson.code import Code import testEmbed as tEB #connect mongoDB def connectDB(): client = MongoClient('localhost', 27017) db = client['faceRecognition'] return db if __name__ == "__main__": db = connectDB() #connect DB faceEmbeddings = db["faceEmbedding"] #mongo shell kodlarini pythonda kullanmak icin #Code kutuphanesi kullanılır mapDB = Code(""" function() { var input = embed; var value = Array.sum(this.encodings.map(function(el,idx) { var mul = Math.abs(el - input[idx]); return mul * mul; })); value = Math.sqrt(value); emit(this._id, {"name":this.name, "score": value }); } """) reduceDB = Code(""" function(key,values) { var output = []; values.forEach(function(value) { value.output.forEach(function(item) { output.push(item); }); }); return { "output": output}; } """) # scope ile mapreduce icin disardan deger tanimlayabiliriz #tEB.embedSener bizim tanimladigimiz matris queryKey = "value.score" result = faceEmbeddings.map_reduce( mapDB, reduceDB, "Output", scope={'embed': tEB.embedSener}) try: for doc in result.find({queryKey: {"$lte": 0.6}}).sort(queryKey, -1): print "name: {0}, score:{1}".format(doc["value"]["name"], doc["value"]["score"]) except: print "boyle bir kullanici yok."
REFERENCE
[1] https://stackoverflow.com/questions/33874022/mongodb-calculate-values-from-two-arrays-sort-and-limit
[2] https://www.tutorialspoint.com/mongodb/mongodb_map_reduce.htm
[3] https://ajay555.wordpress.com/2014/04/19/mongodb-mapreduce-accessing-global-variables-in-map-and-reduce-functions-sent-using-scope/
[4] https://stackoverflow.com/questions/7273379/how-to-use-variables-in-mongodb-map-reduce-map-function?noredirect=1&lq=1
[5] https://stackoverflow.com/questions/21522927/mongodb-mapreduce-scope-referenceerror
[6] https://docs.mongodb.com/manual/aggregation/
Herkese iyi çalışmalar. (Happy work!)