在 mongodb 中知道与 $in 运算符匹配的数组元素的索引吗? [英] In mongodb know index of array element matched with $in operator?

查看:55
本文介绍了在 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 {

            }
        });

优惠券模型架构

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

更新-我在 SO 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 运算符匹配的数组元素的索引吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆