What I am trying to achieve is pretty straightforward. I have a separate microservice with dedicated database for users and database for my app (everything is inside one MongoDB instance).
So, in my app I have comments, which have author field. So, I am trying to figure out how to put the information about author in that field, since the information about them is in another database and $lookup stage can’t do this, because it is looking for a collection in the same database.
That being said, I could manually request an array of required users and then cycle throught comments, replacing the “author” field, but I hope there is a better solution.
As shown in the documentation, you can specify the “from” field for lookup stage in one of two ways:
- String (collection name)
- Map ({ db: ‘db name’, coll: ‘collection name’ })
The second option looks like what I needed, but Mongo driver for Go throws an error:
lookup_author := bson.D{
{
Key: "$lookup", Value: bson.D{
{
Key: "from", Value: bson.M{
"db": "Auth",
"coll": "user",
},
},
{
Key: "localField", Value: "author_id",
},
{
Key: "foreignField", Value: "_id",
},
{
Key: "as", Value: "author",
},
},
},
}
(AtlasError) Error getting from field in $lookup err=Expected 'from' to be string, but got primitive.D instead. Doc = [{from [{db Auth} {coll user}]} {localField author_id} {foreignField _id} {as author}]
So while it seems like this operation is possible, mongo driver for go just don’t support it? Any other ways around? Or just do it manually?