Aggregate query in data api not working like in mongosh

Hello there,

My first topic here So be patient, LoL.

db.getCollection(
  'core.usersAttributes'
).aggregate(
  [
    { $unwind: { path: '$attributes' } },
    { $unwind: { path: '$attributes.values' } },
    {
      $sort: { 'attributes.values.createdAt': -1 }
    },
    {
      $group: {
        _id: {
          id: '$_id',
          attribute: '$attributes.attribute'
        },
        user: { $first: '$user' },
        attribute: {
          $first: '$attributes.attribute'
        },
        value: {
          $first: '$attributes.values.value'
        },
        createdAt: {
          $first: '$attributes.values.createdAt'
        }
      }
    },
    {
      $lookup: {
        from: 'attributes',
        localField: 'attribute',
        foreignField: '_id',
        as: 'attributeInfo'
      }
    },
    {
      $project: {
        _id: '$_id.id',
        user: 1,
        attribute: {
          id: '$attribute',
          title: {
            $arrayElemAt: [
              '$attributeInfo.title.en',
              0
            ]
          },
          value: '$value',
          createdAt: '$createdAt'
        }
      }
    },
    {
      $group: {
        _id: '$_id',
        user: { $first: '$user' },
        attributes: { $push: '$attribute' }
      }
    }
  ],
  { maxTimeMS: 60000, allowDiskUse: true }
);

I need this query to work on the data API. So I converted it to:

{
    "dataSource": "{{dataSource}}",
    "database": "{{dataBase}}",
    "collection": "core.userAttributes",
    "pipeline": [
        { "$unwind": "$attributes" },
        { "$unwind": "$attributes.values" },
        { "$sort": { "attributes.values.createdAt": -1 } },
        {
            "$group": {
                "_id": {
                    "id": "$_id",
                    "attribute": "$attributes.attribute"
                },
                "user": { "$first": "$user" },
                "attribute": { "$first": "$attributes.attribute" },
                "value": { "$first": "$attributes.values.value" },
                "createdAt": { "$first": "$attributes.values.createdAt" }
            }
        },
        {
            "$lookup": {
                "from": "attributes",
                "localField": "attribute",
                "foreignField": "_id", 
                "as": "attributeInfo"
            }
        },
        {
            "$project": {
                "_id": "$_id.id",
                "user": 1,
                "attribute": {
                    "id": "$attribute",
                    "title": { "$arrayElemAt": ["$attributeInfo.title.en", 0] },
                    "value": "$value",
                    "createdAt": "$createdAt"
                }
            }
        },
        {
            "$group": {
                "_id": "$_id",
                "user": { "$first": "$user" },
                "attributes": { "$push": "$attribute" }
            }
        }
    ]
}

The endpoint I’m sending is working normally for other aggregation queries.
With this query, no error (200 response) is returned, as well as no document.

Could you help me find out what is wrong with my data API call?

Thanks in advance!

Hi @Uelinton_Santos - Welcome to the community :slight_smile:

You could try doing the request with just 1 pipeline stage at a time (starting with the first $unwind only). If documents are being returned, add more pipeline stages on until you encounter nothing being returned. This may at least identify which stage in the pipeline the issue could be created from.

Regards,
Jason

Hey @Jason_Tran ,

Thanks for your suggestion.
I ended up discovering by myself the dumb error I made.
As you can see in my original post, I put in the data API call the incorrect name of the collection ‘core.userAttributes’ instead of ‘core.usersAttributes’. :man_facepalming:
Thank you anyway.

Best,

1 Like

Thanks for the update / marking the solution and good catch on the small typo :slight_smile:

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.