In mongodb, do you know the index of the array element mapped to $ in operator?

I am using aggregation with mongoDB, now I am facing a problem, I am trying to match my documents that are present in my input array using the $ in operator. Now I want to know that the lement index from the input array can someone now tell me how I can do this.

My code

var coupon_ids = ["58455a5c1f65d363bd5d2600", "58455a5c1f65d363bd5d2601","58455a5c1f65d363bd5d2602"] couponmodel.aggregate( { $match : { '_id': { $in : coupons_ids }} }, /* Here i want to know index of coupon_ids element that is matched because i want to perform some operation in below code */ function(err, docs) { if (err) { } else { } }); 

Coupon module diagram

 var CouponSchema = new Schema({ category: {type: String}, coupon_name: {type: String}, // this is a string }); 

UPDATE - As user3124885 suggested, aggregation is not better in performance, can someone tell me the performance difference between aggregation and regular query in mongodb. And which one is better?

Update - I read this question on SO mongodb-aggregation-match-vs-find-speed . Here the user himself commented that both of them take the same time, also seeing vlad-z answer, I think aggregation is better. Please, if any of you worked for mongodb Then please tell me what you think about it.

UPDATE - I used json data samples containing 30,000 rows and tried to match with v / s aggregation. Query aggregation detection was performed in 180 ms, where the search query took 220 ms. In addition, I ran $ lookup, it also took no more than 500 ms, so data aggregation is faster than a regular request. Please correct me guys, if any of you tried to use aggregation, and if not, why not?

UPDATE -

I read this post where the user uses the code below as a replacement for $ zip SERVER-20163 , but I do not understand how I can solve my problem using the code below. So can someone please tell me how can I use the code below to solve my problem.

 {$map: { input: { elt1: "$array1", elt2: "$array2" }, in: ["$elt1", "$elt2"] } 

Now someone can help me, it would be very nice for me.

0
source share
2 answers

So, let's say the database collection has the following:

 > db.couponmodel.find() { "_id" : "a" } { "_id" : "b" } { "_id" : "c" } { "_id" : "d" } 

and we want to find the following identifiers in collections

 var coupons_ids = ["c", "a" ,"z"]; 

Then we need to create a dynamic forecast state so that we can project the correct indexes, so we need to match each identifier with the corresponding index

 var conditions = coupons_ids.map(function(value, index){ return { $cond: { if: { $eq: ['$_id', value] }, then: index, else: -1 } }; }); 

Then we can enter this into our aggregation pipeline

 db.couponmodel.aggregate([ { $match : { '_id' : { $in : coupons_ids } } }, { $project: { indexes : conditions } }, { $project: { index : { $filter: { input: "$indexes", as: "indexes", cond: { $ne: [ "$$indexes", -1 ] } } } } }, { $unwind: '$index' } ]); 

The launch above will output each _id and its corresponding index in the coupons_ids array

 { "_id" : "a", "index" : 1 } { "_id" : "c", "index" : 0 } 

However, we can add even more elements to the pipeline at the end and the $index link to get the current consistent index.

+1
source

I think you could do it faster just by retrieving the array and doing a manual search. Remember that aggregation does not give you performance.

0
source

Source: https://habr.com/ru/post/1261194/


All Articles