0

When doing .aggregate on users collection, with $lookup, an oauth field is empty

I've already tried to use pipeline and local/foreign fields

User.aggregate([
        {
          $match: objectQuery,
        },
        {
          $lookup: {
            from: 'OAuthAccessToken',
            as: 'oauth',
            localField: '_id',
            foreignField: 'user'
          }
      ])

There is no any error message, oauth field is empty.

_id in User collection is type string

user in OAuthAccessToken collection is type string

hejkerooo
  • 709
  • 2
  • 7
  • 20
  • 1
    Could you show the data you're running this query on (some sample documents) ? – mickl May 20 '19 at 07:26
  • I'm using `mongoose` in version `4.13.18`, `mongodb` version is `4.0.4` `queryObject` ``` { type: 'USER' } ``` I do not think it matters, records are pulled correctly without lookup `User` sample: ``` _id: '7662a246-9370-47d8-9f31-5dd83a9b9521', username: 'test' ``` `OAuthAccessTokens`: ``` _id: ObjectId("5cdaacc0fb72073f86e25fc5"), user: '7662a246-9370-47d8-9f31-5dd83a9b9521' ``` – hejkerooo May 20 '19 at 07:31
  • 1
    Can you change from `OAuthAccessToken` to `OAuthAccessTokens` in **from** expression. – Ashh May 20 '19 at 07:38
  • Yeah, I've discovered that. Tried with `OAuthAccessTokens`, replaced it to `oauthaccesstokens` and it works right now. Thanks :) – hejkerooo May 20 '19 at 07:41
  • Possible duplicate of [MongoDB: $lookup returns empty array](https://stackoverflow.com/questions/53266339/mongodb-lookup-returns-empty-array) – Ashh May 20 '19 at 07:43
  • MongoDB does lower case, when you create collection, but in lookup - it's not happening – hejkerooo May 20 '19 at 07:51
  • No mongodb does not do anything. It is all about `mongoose`. You can have a look at the answers for more info. – Ashh May 20 '19 at 09:47

0 Answers0