How can I create a view from multiple collections like this.
Hi Tao,
I’d assume that you already know how to create a view from multiple collections but wondering how to do so using MongoDB Go driver.
For all (or most) mongo
shell methods such as db.createView() you can find the underlying database command and use RunCommand to execute the command. Below is an example code of RunCommand
with views command:
db := client.Database("dbname")
pipeline := bson.A{
bson.M{
"$lookup": bson.M{
"from": "targetColl",
"localField": "sourceField",
"foreignField": "targetField",
"as": "newField"},
}}
viewCommand := bson.M{"create": "newViewName", "viewOn": "sourceColl", "pipeline": pipeline }
result := db.RunCommand(context.Background(), viewCommand)
fmt.Println(result.DecodeBytes())
Regards,
Wan.