草庐IT

MongoDB:“$where 不允许出现在 $match 聚合表达式中

coder 2023-11-04 原文

我遇到了这个错误

"$where is not allowed inside of a $match aggregation expression

当我执行以下查询时。

var query = { $and : [
    {"order.orderstatus" : "F"},
    {"partsupp.supplier.nation.name" : { $regex : '^SAUDI ARABIA'} },
    {$where : "this.receiptdate > this.commitdate"}
]};

var multisupp = {
    $where : function() {
        return db.lineitems.findOne({ $and : [
            { "order.orderkey" : this.order.orderkey },
            { "partsupp.supplier.suppkey" : { "$ne" : this.partsupp.supplier.suppkey} }
        ]}) != null;
    }
};

var onlyfail = {
    $where : function() {
        return db.lineitems.findOne({ $and : [
            { "order.orderkey" : this.order.orderkey },
            { "partsupp.supplier.suppkey" : { "$ne" : this.partsupp.supplier.suppkey} },
            { $where : "this.receiptdate > this.commitdate" }
            ]}) == null;
    }
};

res = db.lineitems.aggregate([
    { $match : query},
    { $match : multisupp},
    { $match : onlyfail},
    { $project : {
        _id : 0,
        s_name : "$partsupp.supplier.name" } },
    { $group : {
        _id : "$s_name",
        numwait : { $sum : 1} } },
    { $sort : { numwait : -1, _id : 1} }
]);

集合模式是这样的。 (这个集合有一个巨大的模式,但我想这对那个问题并不重要)

{
    "linenumber": 6,
    "quantity": 17,
    "extendedprice": 21085.78,
    "discount": 0.08,
    "tax": 0.01,
    "returnflag": "R",
    "linestatus": "F",
    "shipdate": "Wed Oct 20 00:00:00 BST 1993",
    "commitdate": "Thu Dec 09 00:00:00 GMT 1993",
    "receiptdate": "Sun Nov 14 00:00:00 GMT 1993",
    "shipinstruct": "COLLECT COD",
    "shipmode": "REG AIR",
    "comment": "ns. carefully regular asym",
    "order": {
        "orderkey": 535110,
        "orderstatus": "F",
        "totalprice": 200883.33,
        "orderdate": "Fri Oct 01 00:00:00 BST 1993",
        "orderpriority": "1-URGENT",
        "clerk": "Clerk#000000110",
        "shippriority": 0,
        "comment": "furiously against the regular ideas; quickly regular dep",
        "order": {
            "custkey": 6464,
            "name": "Customer#000006464",
            "address": "eF9E6ScHCw9,z8nF0py9 ySlB0 iHTIEEZRWl6H",
            "phone": "11-870-572-9943",
            "acctbal": 5468.53,
            "mktsegment": "FURNITURE",
            "comment": "telets could are quickly regular packages. fluffily iro",
            "customer": {
                "nationkey": 1,
                "name": "ARGENTINA",
                "comment": "al foxes promise slyly according to the regular accounts. bold requests alon",
                "region": {
                    "regionkey": 1,
                    "name": "AMERICA",
                    "comment": "hs use ironic, even requests. s"
                }
            }
        }
    },
"partsupp": {
    "availqty": 9067,
    "supplycost": 441.56,
    "comment": ". bold instructions are carefully blithely final foxes. slyly unusual ideas wake slowly aroun",
    "part": {
        "partkey": 340,
        "name": "sienna purple lawn coral navy",
        "mfgr": "Manufacturer#2",
        "brand": "Brand#23",
        "type": "STANDARD BRUSHED TIN",
        "size": 3,
        "container": "JUMBO BOX",
        "retailprice": 1240.34,
        "comment": "l ideas wake. quic"
    },
    "supplier": {
        "suppkey": 91,
        "name": "Supplier#000000091",
        "address": "YV45D7TkfdQanOOZ7q9QxkyGUapU1oOWU6q3",
        "phone": "13-604-986-9056",
        "acctbal": 6255.87,
        "comment": "nstructions use carefully according to the special packages: quickly silent th",
        "nation": {
            "nationkey": 3,
            "name": "CANADA",
            "comment": "eas hang ironic, silent packages. slyly regular packages are furiously over the tithes. fluffily bold",
            "region": {
                "regionkey": "1",
                "name": "AMERICA",
                "comment": "hs use ironic, even requests. s"
            }
        }
    }
}

有人可以帮我解决这个问题吗?谢谢。

最佳答案

如错误所述,聚合查询中不能有 $where。您必须将其更改为:

var query = { $and : [
  {"order.orderstatus" : "F"},
  {"partsupp.supplier.nation.name" : { $regex : '^SAUDI ARABIA'} },
  {receiptdate : { $gt : commitdate}
]};

关于MongoDB:“$where 不允许出现在 $match 聚合表达式中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36414780/

有关MongoDB:“$where 不允许出现在 $match 聚合表达式中的更多相关文章

  1. ruby 正则表达式 - 如何替换字符串中匹配项的第 n 个实例 - 2

    在我的应用程序中,我需要能够找到所有数字子字符串,然后扫描每个子字符串,找到第一个匹配范围(例如5到15之间)的子字符串,并将该实例替换为另一个字符串“X”。我的测试字符串s="1foo100bar10gee1"我的初始模式是1个或多个数字的任何字符串,例如,re=Regexp.new(/\d+/)matches=s.scan(re)给出["1","100","10","1"]如果我想用“X”替换第N个匹配项,并且只替换第N个匹配项,我该怎么做?例如,如果我想替换第三个匹配项“10”(匹配项[2]),我不能只说s[matches[2]]="X"因为它做了两次替换“1fooX0barXg

  2. ruby-on-rails - 相关表上的范围为 "WHERE ... LIKE" - 2

    我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que

  3. ruby-on-rails - RSpec:避免使用允许接收的任何实例 - 2

    我正在处理旧代码的一部分。beforedoallow_any_instance_of(SportRateManager).toreceive(:create).and_return(true)endRubocop错误如下:Avoidstubbingusing'allow_any_instance_of'我读到了RuboCop::RSpec:AnyInstance我试着像下面那样改变它。由此beforedoallow_any_instance_of(SportRateManager).toreceive(:create).and_return(true)end对此:let(:sport_

  4. ruby - 正则表达式将非英文字母匹配为非单词字符 - 2

    @raw_array[i]=~/[\W]/非常简单的正则表达式。当我用一些非拉丁字母(具体来说是俄语)尝试时,条件是错误的。我能用它做什么? 最佳答案 @raw_array[i]=~/[\p{L}]/使用西里尔字符进行测试。引用:http://www.regular-expressions.info/unicode.html#prop 关于ruby-正则表达式将非英文字母匹配为非单词字符,我们在StackOverflow上找到一个类似的问题: https://

  5. ruby - 正则表达式在哪个位置失败? - 2

    我需要一个非常简单的字符串验证器来显示第一个符号与所需格式不对应的位置。我想使用正则表达式,但在这种情况下,我必须找到与表达式相对应的字符串停止的位置,但我找不到可以做到这一点的方法。(这一定是一种相当简单的方法……也许没有?)例如,如果我有正则表达式:/^Q+E+R+$/带字符串:"QQQQEEE2ER"期望的结果应该是7 最佳答案 一个想法:你可以做的是标记你的模式并用可选的嵌套捕获组编写它:^(Q+(E+(R+($)?)?)?)?然后你只需要计算你获得的捕获组的数量就可以知道正则表达式引擎在模式中停止的位置,你可以确定匹配结束

  6. ruby - 使用 rbenv 和 ruby​​-build 构建 ruby​​ 失败,出现 undefined symbol : SSLv2_method - 2

    我正在尝试在配备ARMv7处理器的SynologyDS215j上安装ruby​​2.2.4或2.3.0。我用了optware-ng安装gcc、make、openssl、openssl-dev和zlib。我根据README中的说明安装了rbenv(版本1.0.0-19-g29b4da7)和ruby​​-build插件。.这些是随optware-ng安装的软件包及其版本binutils-2.25.1-1gcc-5.3.0-6gconv-modules-2.21-3glibc-opt-2.21-4libc-dev-2.21-1libgmp-6.0.0a-1libmpc-1.0.2-1libm

  7. ruby - 有没有办法从 ruby​​ case 语句中访问表达式? - 2

    我想从then子句中访问c​​ase语句表达式,即food="cheese"casefoodwhen"dip"then"carrotsticks"when"cheese"then"#{expr}crackers"else"mayo"end在这种情况下,expr是食物的当前值(value)。在这种情况下,我知道,我可以简单地访问变量food,但是在某些情况下,该值可能无法再访问(array.shift等)。除了将expr移出到局部变量然后访问它之外,是否有直接访问caseexpr值的方法?罗亚附注我知道这个具体示例很简单,只是一个示例场景。 最佳答案

  8. ruby - 正则表达式 - 排除一个字符 - 2

    这是一个例子:s="abcd+subtext@example.com"s.match(/+[^@]*/)Result=>"+subtext"问题是,我不想在其中包含“+”。我希望结果是“潜台词”,没有+ 最佳答案 您可以在正则表达式中使用括号来创建匹配组:s="abcd+subtext@example.com"s=~/\+([^@]*)/&&$1=>"subtext" 关于ruby-正则表达式-排除一个字符,我们在StackOverflow上找到一个类似的问题:

  9. ruby - 如何遍历 Ruby 中所有正则表达式匹配的字符串? - 2

    我们有一个字符串:“”这个正则表达式://i如何从当前字符串中获取所有匹配项? 最佳答案 "".scan(//)参见scan在ruby​​-docs上 关于ruby-如何遍历Ruby中所有正则表达式匹配的字符串?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/6857852/

  10. ruby - 允许主机名包含下划线的 URI.parse 的替代方法 - 2

    我正在使用DMOZ的listofurltopics,其中包含一些具有包含下划线的主机名的url。例如:608609TheOuterHeaven610InformationandimagegalleryofMcFarlane'sactionfiguresforTrigun,Akira,TenchiMuyoandotherJapaneseSci-Fianimations.611Top/Arts/Animation/Anime/Collectibles/Models_and_Figures/Action_Figures612虽然此url可以在网络浏览器中使用(或者至少在我的浏览器中可以使用:

随机推荐