Join data from collections in different databases (go griver)?

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:

  1. String (collection name)
  2. 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?

Hey @Andrew_Rusinas , the Go Driver doesn’t have a preference for how you construct queries, the error you are getting is a response from an Atlas Server (i.e. AtlasError). The documentation you provided is specifically for querying federated data. Does running your query in mongosh result in the same error?

{
  $lookup: {
    from: {
      db: "Auth",
      coll: "user",
    },
    localField: "author_id",
    foreignField: "_id",
    as: "author",
  },
}

Here is the documentation for the from field: https://www.mongodb.com/docs/manual/reference/operator/aggregation/lookup/#std-label-lookup-eq-from:

[from] specifies the collection in the same database to perform the join with.

The Go Driver will construct the database reference from the aggregate chain:

// The database we are operating the aggregate on is "mydb", provide the 
// `from` field with the collection name.
myclient.Database("mydb").Aggregate(context.Background(), lookupPipeline)