Why my expression who contents array doesn't detect as array?

30 views
Skip to first unread message

Дмитрий Бачинин

unread,
May 21, 2019, 5:55:26 PM5/21/19
to mongodb-user
Hi!
I have a problem. Maybe can someone tell me about MongoDB's aggregations?
I have the API by Mongoid and Sinatra.
And I have a CustomerStatus object on 'customer_status' collection. CustomerStatus have 'status' field.
I need to get total customers whose status in CRM was originally a status Guest compared to how many of
those who converted to status Member.
Count the total number of 'guest' and 'member' with the same id's as the guests.
I create this aggregation:

CustomerStatus.collection.aggregate([
    {
        '$sort': {'self_customer_status_created_at': 1}
    }, 
    {'$match': 
        {
            'self_customer_status_created_at': 
            {
                "$gte": Time.parse('2017-01-17').beginning_of_month, 
                "$lte": Time.parse('2017-01-17').end_of_month 
            }
        } 
    }, 
    {
        "$facet": {
            "guests": 
            [ 
                { 
                    "$group": {
                        "_id": "$_id", 
                        "data": {
                            '$first': '$$ROOT'
                        } 
                    } 
                }, 
                {
                    "$match": {
                        "data.status": "guest"
                    }
                }, {
                    "$group": {
                        "_id":nil, 
                        "array":{
                            "$push": "$data.self_customer_status_id" 
                        } 
                    } 
                }, 
                { 
                    "$project":{
                        "array": 1,
                        "_id":0
                    } 
                }
            ], "members": 
            [ 
                { 
                    "$group": {
                        "_id": "$_id", "data": {
                            '$last': '$$ROOT'
                        } 
                    } 
                }, 
                {
                    "$match": {
                        "data.status": "member",
                        "data.self_customer_status_id": {
                            "$in":
                                [
                                    "$guests.array"
                                ]
                        } 
                    }
                } 
            ] 
        } 
    }, {
        "$project": 
        { 
            "members": 1, 
            "guests.array": 1 
        } 
        } 
    ]
).as_json

But guests.array still doesn't read as array

And return error Mongo::Error::OperationFailure: $in needs an array (2)


If I change {"$in": ["$guests.array" ] } to {"$in": ["21128"]  } all is works.

How do I extract array from 'guests.array'?

Wan Bachtiar

unread,
May 23, 2019, 3:34:44 AM5/23/19
to mongodb-user

How do I extract array from ‘guests.array’?

Hi,

To clarify your question further, could you provide the following:

  • MongoDB server version
  • Example documents from the collection

Regards,
Wan.

Reply all
Reply to author
Forward
0 new messages