Need to get for each document some info from all containing collections

  Kiến thức lập trình

title seems crazy, let me show a simple example. By the way, I searched StackOverflow but it does not seem like anything works despite trying aggregate, lookup etc.

Collection House:

{
    "_id": ObjectId('0001'),
    "fruit": "apple",
    "entries": [
        DBRef('room', ObjectId('1234')),
        DBRef('room', ObjectId('5678'))
    ]
}

Collection rooms:

{
    "_id": ObjectId('1234'),
    "some": "data"
}

I know want all the rooms with a fruit array (because a room could be in multiple houses; yeah, the metaphor breaks here, but…) like:

[
    {
        "some": "data",
        "fruits": ["apple"]
    }
]

So I tried aggregating on rooms, doing a lookup on the house, but as I need to search the array, I could not use a normal lookup but one with pipeline. Within the pipeline I would need to create some match-making BUT within that (using map) I cannot get the damn ObjectId out of the DBRef. I know, DBRef is apparently outdated but it’s the DB we have now and I need to work with.

Any help? DB is on version 4.0.22

LEAVE A COMMENT