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'?