Lookup for array returns only one element

Hi,
I’m using an aggregation to replace ObjectIds in one collection with entries in another collection. The stories collection has the following structure:

{
  _id: new ObjectId("62e82d326f664edd9b96f33a"),
  title: 'Test 0',
  description: '...',
  user_id: new ObjectId("62e82d326f664edd9b96f330"),
  entries: [
    new ObjectId("62e82d6d0f5748cbefb0a4a3"),
    new ObjectId("62e82f8c1b832b9714edf574"),
    new ObjectId("62e82f8c1b832b9714edf577"),
    new ObjectId("62e82fcc5c3e92e184d5c733"),
    new ObjectId("62e830535297f48e35d47629")
  ]

The entries array are the _ids of the entries in the entries collection:

{
      _id: new ObjectId("62e830535297f48e35d47629"),
      beginTime: 2022-08-01T19:58:11.772Z,
      endTime: 2022-08-01T19:58:11.772Z,
      content: [Object],
      mapView: [Object],
      stories: [Array]
    }

My aggregation is the following:

[
    {
      $match: {
        _id: new ObjectId('62e82d326f664edd9b96f33a')
      }
    },
    {
      $lookup: {
        from: 'entries',
        localField: 'entries',
        foreignField: '_id',
        as: 'entries2'
      }
    }
  ]

I would expect, that the result (entries2) contains 5 entries, but instead the array has only one (the last element):

{
  _id: new ObjectId("62e82d326f664edd9b96f33a"),
  title: 'Test 0',
  description: '...',
  user_id: new ObjectId("62e82d326f664edd9b96f330"),
  entries: [
    new ObjectId("62e82d6d0f5748cbefb0a4a3"),
    new ObjectId("62e82f8c1b832b9714edf574"),
    new ObjectId("62e82f8c1b832b9714edf577"),
    new ObjectId("62e82fcc5c3e92e184d5c733"),
    new ObjectId("62e830535297f48e35d47629")
  ],
  entries2: [
    {
      _id: new ObjectId("62e830535297f48e35d47629"),
      beginTime: 2022-08-01T19:58:11.772Z,
      endTime: 2022-08-01T19:58:11.772Z,
      content: [Object],
      mapView: [Object],
      stories: [Array]
    }
  ]
}

I get the same result with compass or running it on nodejs.
Any ideas?

Carsten

Your $lookup should work.

mongosh> db.stories.find()
{ _id: 1, entries: [ 2, 3, 4 ] }
mongosh> db.entries.find()
{ _id: 2, title: 'two' }
{ _id: 3, title: 'three' }
{ _id: 4, title: 'four' }
mongosh> db.stories.aggregate( {
      $lookup: {
        from: 'entries',
        localField: 'entries',
        foreignField: '_id',
        as: 'entries2'
      }
    })
{ _id: 1,
  entries: [ 2, 3, 4 ],
  entries2: 
   [ { _id: 2, title: 'two' },
     { _id: 3, title: 'three' },
     { _id: 4, title: 'four' } ] }

Sometimes, we think the documents are in the $lookup-ed up collection because we forget to check if the data type is the same. It is possible that in one collection you a string and in the other collection you have an ObjectId.

This or the entries are really not in the collection.

Hi Steeve,
thank you for your answer. I re-generated my test data and now the aggregation works as expected. Unclear what was wrong, but I discovered that the Aggregation view in Compass had old data shown, so maybe I used old IDs.
Thanks anyway.

1 Like