ma_jafari
ma_jafari

Reputation: 1059

MongoDB 4.4: input to $filter must be an array not long

I've implemented the following aggregation in MongoDB 4.4.2 and it works fine:

[{
    $match: {
        "$expr": {
            "$and": [{
                "$not": "$history_to"
            }, ],
        },

    }
}, {
    $unwind: {
        "path": "$used",
        "preserveNullAndEmptyArrays": true,
    }
}, {
    $project: {
        "ticket": "$$ROOT",
        "status": {
            "$map": {
                "input": {
                    "$filter": {
                        "input": "$status",
                        "as": "cstatus",
                        "cond": {
                            "$not": "$$cstatus.history_to",
                        },
                    },
                },
                "as": "status",
                "in": "$$status.value",
            },
        },
    }
}]

But when I try it in MongoDB 4.4.4 I encounter input to $filter must be an array not long.

If it's any help, I figured that the cause of this error has something to do with:

"cond": {
    "$not": "$$cstatus.history_to",
},

Since when I comment $not it works fine; At first I thought that maybe $not is not supported anymore but it is supported so I'm out of ideas.

Some example documents

{
    "_id" : ObjectId("6083ca1ce151beea45602e5d"),
    "created_at" : ISODate("2021-04-24T07:34:52.947Z"),
    "ticket_id" : ObjectId("6083ca1c68badcedddd875ba"),
    "expire_at" : ISODate("2021-04-24T19:30:00Z"),
    "history_from" : ISODate("2021-04-24T07:34:52.992Z"),
    "created_by" : ObjectId("604df7857d58ab06685ed02e"),
    "serial" : "116627138",
    "status" : [
        {
            "history_from" : ISODate("2021-04-24T07:34:52.985Z"),
            "history_created_by" : ObjectId("604df7857d58ab06685ed02e"),
            "value" : NumberLong(1)
        }
    ],
    "history_created_by" : ObjectId("604df7857d58ab06685ed02e")
},

{
    "_id" : ObjectId("60713b0fe151beea45602e56"),
    "created_by" : ObjectId("604df7857d58ab06685ed02e"),
    "ticket_id" : ObjectId("60713b0f68badcedddd875b8"),
    "created_at" : ISODate("2021-04-10T05:43:43.228Z"),
    "history_created_by" : ObjectId("604df7857d58ab06685ed02e"),
    "status" : [
        {
            "history_created_by" : ObjectId("604df7857d58ab06685ed02e"),
            "value" : NumberLong(1),
            "history_from" : ISODate("2021-04-10T05:43:43.277Z")
        }
    ],
    "serial" : "538142578",
    "expire_at" : ISODate("2021-04-10T19:30:00Z"),
    "history_from" : ISODate("2021-04-10T05:43:43.281Z"),}


Upvotes: 1

Views: 425

Answers (1)

turivishal
turivishal

Reputation: 36104

Your query looks good as per your example documents you can check playground,

But when I try it in MongoDB 4.4.4, I encounter input to $filter must be an array not long.

It is not any MongoDB version specific issue, The error says provided field status as input in $filter is not array type it is long type, and $filter input should be an array type.

Definitely there are/is some document(s) having non array value in status field.

If you want to check you can match condition before $filter operation,

{ $match: { status: { $type: "array" } } } // 4 = "array"

This will filter documents by status, it should be an array type.

Upvotes: 1

Related Questions