2

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

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 {

            }
        });

Couponmodel Schema

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

UPDATE- As suggested by user3124885 that aggregation is not better in performance, can anyone please tell me the performance difference between aggregation and normal 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 take same time, also by seeing vlad-z answer i think aggregation is better. Please if anyone of you have worked on mongodb Then please tell me what are your opinion about this.

UPDATE- I used sample json data containing 30,000 rows and tried match with aggregation v/s find query aggregation got executed in 180 ms where find query took 220ms. ALso i ran $lookup it is also taking not much than 500ms so think aggregation is bit faster than normal query. Please correct me guys if any one of you have tried using aggregation and if not then why ??

UPDATE-

I read this post where user uses below code as a replacement of $zip SERVER-20163 but i am not getting how can i solve my problem using below code. So can anybody please tell me how can i use below code to solve my issue.

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

Now can anyone please help me, it would be really be a great favor for me.

7
  • Why not just do a find({_id: {$in: coupons_ids}}) and then search coupons_ids for the _id values in the returned doc(s)? Commented Dec 12, 2016 at 1:12
  • because using aggregation server overload gets reduced that's why thining about using aggregation ?? Also can you please tell me is aggregation slower or lot slower than normal query or what ?? Also i ran some cases where i have 30000 sample data and aggregation was there little bit faster than normal query.Please correct me if i am gong in wrong direction ?? Commented Dec 12, 2016 at 16:01
  • What are you wanting to do within the aggregation query after matching the elements? Commented Dec 12, 2016 at 16:54
  • @KevinSmith i want to return coupon_ctr at same index as in coupon_id which is matched Commented Dec 12, 2016 at 16:58
  • Have you got an example of the structure of the documents inside couponmodel collection Commented Dec 12, 2016 at 17:12

3 Answers 3

1

So say we have the following in the database collection:

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

and we wish to search for the following ids in the collections

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

We'll then have to build up a dynamic projection state so that we can project the correct indexes, so we'll have to map each id to its corresponding index

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

Then we can then inject this in to 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' }
]);

Running the above will now output each _id and it's corresponding index within the coupons_ids array

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

However we can also add more items in to the pipeline at the end and reference $index to get the current matched index.

Sign up to request clarification or add additional context in comments.

12 Comments

sorry but you misunderstood my question i want coupon_ctr value to return along with _id.
You're question has nothing related to coupon_ctr within it, can you explain in more details?
I am so sorry but let me explain, actually your are printing index from database but i want index according to coupon_ids which is internal array which i am passing so output should be this { "_id" : "a", "index" : 1 } { "_id" : "c", "index" : 0 }
That is the index from the array... we've just injected in some projections so we can work out the correct location in the array... If you look at my output it is the same as what you describe
It's creating the conditions for each match to return the index so it can be placed in the projection :-)
|
0

I think you could do it in a faster way simply retrieving the array and search manually. Remember that aggregation don't give you performance.

15 Comments

yes but computation on server makes server busy which is also a major problem, that's why trying to use aggregation. Please enlighten me if you think i am thinking it in wrong direction.
also is agregation much slower than normal query, because i have seen it on SO questions then they both take same time to run a query, please coorect me if am wrong ?? also thanks for posting an answer here.
No aggregation is the lowest operation in MongoDB instead of the other operations. In your case, if you haven't other operations to do in the aggregation, I advertise you simply to :
also aggreagtion is not lowest as map reduce is multiple time sslower than aggregation.
I saw your updated post. MongoDB is a NOSQL database, so you have to create the database structure in non-relational way.
|
0

//$match,$in,$and

    $match:{
        $and:[        
        {"uniqueID":{$in:["CONV0001"]}},
        {"parentID":{$in:["null"]}},
        ]
       }
   }])

1 Comment

@devpato There is no link in the answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.