本文介绍了在mongodb中知道与$ in运算符匹配的数组元素的索引吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在在mongoDB中使用聚合,现在我在这里面临一个问题,我正在尝试使用$ in运算符来匹配输入数组中存在的文档.现在我想从输入数组中了解元素的索引,现在有人可以告诉我如何做到这一点.

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.

我的代码

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架构

Couponmodel Schema

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

更新-正如user3124885所建议的那样,聚合的性能不是更好,任何人都可以告诉我聚合和mongodb中普通查询之间的性能差异.哪个更好?

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 ??

更新-我在 mongodb-aggregation-match-vs-find-speed 上阅读了这个问题>.用户自己在这里评论说,两者都花相同的时间,也可以通过查看 vlad-z 来回答,我认为聚合效果更好.如果您中有人在mongodb上工作过,请告诉我.

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.

更新-我使用了包含30,000行的json样本数据,并尝试与聚合v/s进行匹配.查找查询聚合在180毫秒内执行,其中查找查询花费了220毫秒.我还运行了$ lookup,它也花费不超过500毫秒,因此认为聚合比普通查询快一点.如果您中有人尝试使用聚合,请纠正我,如果没有尝试,为什么???

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 ??

更新-

我阅读了这篇文章,其中用户使用下面的代码替换$ zip SERVER-20163 ,但是我不知道如何使用下面的代码解决我的问题.所以任何人都可以告诉我如何使用下面的代码来解决我的问题.

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.

推荐答案

所以说我们在数据库集合中有以下内容:

So say we have the following in the database collection:

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

,我们希望在集合中搜索以下ID

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

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

然后我们必须建立动态投影状态,以便可以投影正确的索引,因此我们必须将每个id映射到其对应的索引

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' }
]);

运行上面的命令现在将输出每个_id及其在coupons_ids数组中的对应索引

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 }

但是,我们也可以在管道的末尾添加更多项,并引用$index以获得当前匹配的索引.

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

这篇关于在mongodb中知道与$ in运算符匹配的数组元素的索引吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 14:31