首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于抽象查询和传递变量/参数的MongoDB视图vs函数

用于抽象查询和传递变量/参数的MongoDB视图vs函数
EN

Stack Overflow用户
提问于 2020-11-19 05:35:19
回答 1查看 134关注 0票数 0

我讨厌冒险问重复的问题,但这可能与没有任何明确解决方案的Passing Variables to a MongoDB View不同。

以下是查找IP地址16778237的国家/地区的查询。(在此查询的范围之外,有一个将IPV4地址转换为数字的公式。)

我想知道我们是否可以从NodeJS代码中抽象出这个查询,并创建一个视图,这样就可以从NodeJS调用该视图。但是字段ipFrom和ipTo被编入了索引,以便对集合中数以百万计的文档快速运行查询,因此我们不能将所有行返回到NodeJS并在那里进行过滤。

在MSSQL中,这可能必须是一个存储过程,而不是视图。我只是想了解在MongoDB中可以实现的功能。我知道有些函数是用JavaScript编写的。那是我需要看的地方吗?

代码语言:javascript
复制
db['ip2Locations'].aggregate(
    {
        $match: 
        {
            $and: [
                {
                    "ipFrom": {
                        $lte: 16778237
                    }
                },
                {
                    "ipTo": {
                        $gte: 16778237
                    }
                },
                {
                    "active": true
                }
            ],
                    $comment: "where 16778237 between startIPRange and stopIPRange and the row is 'Active',sort by createdDateTime, limit to the top 1 row, and return the country"
        }
    },
    {
        $sort: 
        {
            'createdDateTime': - 1
        }
    },
    {
        $project: 
        {
            'countryCode': 1
        }
    },
    {
        $limit: 1
    }
)

第2部分-经过更多的研究和实验,我发现这是可能的,并成功运行,但随后请参阅下面的尝试制作视图。

代码语言:javascript
复制
var ipaddr = 16778237
db['ip2Locations'].aggregate(
    {
        $match: 
        {
            $and: [
                {
                    "ipFrom": {
                        $lte: ipaddr
                    }
                },
                {
                    "ipTo": {
                        $gte: ipaddr
                    }
                },
                {
                    "active": true
                }
            ],
                    $comment: "where 16778237 between startIPRange and stopIPRange and the row is 'Active',sort by createdDateTime, limit to the top 1 row, and return the country"
        }
    },
    {
        $sort: 
        {
            'createdDateTime': - 1
        }
    },
    {
        $project: 
        {
            'countryCode': 1
        }
    },
    {
        $limit: 1
    }
)

如果我尝试创建一个带有"var“的视图,如下所示;

db.createView("ip2Locations_vw-lookupcountryfromip","ip2Locations",[ var ipaddr = 16778237 db‘ip2Locations’..aggregate(

我得到了错误:

代码语言:javascript
复制
[Error] SyntaxError: expected expression, got keyword 'var'

在我上面提供的链接中,我认为这个人试图弄清楚$$user变量是如何工作的(这里没有示例:https://docs.mongodb.com/manual/reference/aggregation-variables/)。该页面引用了$let,但从未显示两者是如何协同工作的。我在这里找到了一个例子:变量上的https://www.tutorialspoint.com/mongodb-query-to-set-user-defined-variable-into-query,但不是$$variables。我

db.createView("ip2Locations_vw-lookupcountryfromip","ip2Locations",[db‘ip2Locations’.聚合( ...etc..."ipFrom":{ $lte:$$ipaddr }

我尝试了ipaddr、$ipaddr和$$ipaddr,它们都给出了这个错误的变体:

代码语言:javascript
复制
[Error] ReferenceError: $ipaddr is not defined

在一个完美的世界里,人们可以这样做:

代码语言:javascript
复制
   get['ip2Locations_vw-lookupcountryfromip'].find({$let: {'ipaddr': 16778237})

或者类似的。

我了解到使用存储在MongoDB (How to use variables in MongoDB query?)中的Javascript是可能的,但我必须重新阅读这一点;似乎一些博客对此发出了警告。

我还没有找到一个使用$$user-variables的工作示例,还在寻找。

EN

回答 1

Stack Overflow用户

发布于 2020-11-19 13:39:05

Interpretation

您希望从一些服务器端代码中查询视图,并向其传递一个变量。

Context

我们可以使用外部变量来重新计算视图吗?采用以下管道:

代码语言:javascript
复制
var pipeline = [{ $group:{ _id:null, useless:{ $push:"$$NOW" } } }] 

我们可以使用$$传递system variables。我们也可以定义用户变量,但是用户定义的变量是由:

  • 收集数据
  • 系统变量。

另外,请尊重您的Part2:

变量var variable="what"将只计算一次。重新定义variable="whatever"在视图中没有区别,它使用"what“。

结论

只能使用系统变量或依赖于这些系统变量或集合数据的用户变量重新计算视图。

Added an answer to the post you link too.

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64901765

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档