[GODRIVER-1771] mongo-driver/mongo 在使用collection.Aggregate时 出错 Created: 20/Oct/20  Updated: 27/Oct/23  Resolved: 19/Nov/20

Status: Closed
Project: Go Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: quiet king Assignee: Kevin Albertson
Resolution: Gone away Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: PNG File 3S}Y5@BVB6$3_~QK1(4Z)IJ.png     PNG File B]0U@4Q(A9HA${_{K~`PRWP.png     PNG File WRXIJKGSZ28GWKJ9L7D`~VR.png    

 Description   

当我 使用 mongo-driver/mongo 中的 collection.Aggregate 方法时,会报错误, 直接使用命令语句 并没有问题



 Comments   
Comment by Backlog - Core Eng Program Management Team [ 19/Nov/20 ]

There hasn't been any recent activity on this ticket, so we're resolving it. Thanks for reaching out! Please feel free to comment on this if you're able to provide more information.

Comment by Kevin Albertson [ 04/Nov/20 ]

Hello! Thank you for filing this report.

That error is comes from attempting to decode a string as a primitive.ObjectID type using the default ObjectID decoder.

I can reproduce this error by inserting the document: {_id: "info"} into the collection "test.test" and running the following:

package main
 
import (
	"context"
	"fmt"
	"log"
 
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)
 
type MyStruct struct {
	ID primitive.ObjectID `bson:"_id"`
}
 
func main() {
	client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI("mongodb://localhost:27017"))
	if err != nil {
		fmt.Println(err)
		return
	}
 
	matchStage := bson.D{{"$match", bson.D{}}}
	cursor, err := client.Database("test").Collection("test").Aggregate(context.TODO(), mongo.Pipeline{matchStage})
	if err != nil {
		log.Fatalf("aggregate error: %v", err)
	}
 
	if !cursor.Next(context.TODO()) {
		log.Fatalf("no results")
	}
 
	res := &MyStruct{}
	err = cursor.Decode(&res)
	if err != nil {
		log.Fatalf("decode error: %v", err)
	}
}

Can you provide additional details on how you are attempting to decode the documents returned from the Cursor returned by Aggregate? If possible a short, self-contained, compilable example showing your issue would be helpful.

Generated at Thu Feb 08 08:37:06 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.