1

I have a mongoose collection that looks like this

[{
  _type:'video',
  _id:'video1',
  status:'active'
},
{
  _type:'video',
  _id:'video2',
  status:'active'
}]

And another collection like this

[{
  _type:'product',
  _id:'product1',
  items:['video1']
}]

How can i use mongoose aggregate to find all documents with a _type of 'video' that are referenced in the field 'items' of any document with a _type of 'product'

What i want as a result

[{
  _type:'video',
  _id:'video1',
  status:'active'
}]

this is what i have so far, but 'connectedVideos' is always an empty array

[{
    "$match":{
       "_type":"product"
    }
},

{
    "$project": {
        "items":true,
        "_id":true,
        "title":true
    }
},
{
    "$unwind":"$items"
},
{
    "$lookup": {
        "from": "videos",
        "localField": "items",
        "foreignField": "_id",
        "as": "connectedVideos"
    }
}
]
Cade Embery
  • 433
  • 1
  • 5
  • 18
  • Possible duplicate of [$lookup on ObjectId's in an array](http://stackoverflow.com/questions/34967482/lookup-on-objectids-in-an-array) – s7vr Feb 13 '17 at 03:09
  • I don't think that is the case, The question you have linked demonstrates how to do the opposite of what i'm looking for. I need to find all 'video' documents that are contained in the 'items' field of a set of 'product' documents, The question you referred would be if i wanted to find all 'product' documents that contain selected 'video' documents? – Cade Embery Feb 13 '17 at 03:48
  • I mean you just need to `$unwind` at the end to flatten the videos array. Isnt this what you are after ? `[{ "$match": { "_type": "product" } }, { "$unwind": "$items" }, { $lookup: { from: "videos", localField: "items", foreignField: "_id", as: "videos" } }]`.This will give you all videos for each product. – s7vr Feb 13 '17 at 04:02
  • I've updated with what my code currently is, which is almost identical to what you just posted, but 'connectedVideos' is always an empty Array even though they have object ids inside, so maybe its another issue because it appears that $lookup just plain doesn't work with my setup atm – Cade Embery Feb 13 '17 at 04:44
  • Your query should run fine on the data set you provided. Is your collection name `videos`? The expected output I see is `{ "_id" : "product1", "items" : "video1", "connectedVideos" : [ { "_id" : "video1", "_type" : "video", "status" : "active" } ] }` – s7vr Feb 13 '17 at 04:50
  • Yeah i just get an empty array, { "_id": "product1", "_type": "product", "items": "video1", "connectedVideos": [] }, so it must be another issue, ill keep looking into it, thanks for your help though, its good to know that is what i should be expecting and im not going insane haha – Cade Embery Feb 13 '17 at 04:54
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/135545/discussion-between-cade-embery-and-veeram). – Cade Embery Feb 13 '17 at 05:02

1 Answers1

0

It turns out that the $lookup posted above SHOULD work, but doesn't and it seems that it is due to my localField being a string by my foreignField being an ObjectId so i have posted a more refined question detailing the issue here

$lookup localField as string but foreignField as ObjectId

Community
  • 1
  • 1
Cade Embery
  • 433
  • 1
  • 5
  • 18