MongoDB:" $不允许在$ match聚合表达式中

时间:2016-04-04 23:48:52

标签: mongodb

我收到了这个错误

  

" $ $匹配聚合表达式中不允许的地方

当我执行以下查询时。

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"
            }
        }
    }
}

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

1 个答案:

答案 0 :(得分:2)

如错误所示,您在聚合查询中无法$where。你必须将其改为:

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