[GODRIVER-2613] Error EOF on decode aggregate cursor Created: 26/Oct/22  Updated: 27/Oct/23  Resolved: 16/Nov/22

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

Type: Bug Priority: Unknown
Reporter: Javad Rajabzadeh Assignee: Preston Vasquez
Resolution: Gone away Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: PNG File Screenshot from 2022-10-26 11-52-18.png    

 Description   

I want decode one result on one object but got error EOF on cursor decode.

 



 Comments   
Comment by Preston Vasquez [ 22/Sep/23 ]

diamondheadqueen@gmail.com Apologies for any inconvenience. Are you calling Cursor.Decode within the context of Cursor.Next ?

From the Cursor.Decode documentation:

Decode will unmarshal the current document into val

From the Cursor.Next documentation:

Next gets the next document for this cursor. It returns true if there were no errors and the cursor has not been exhausted.

That is, you must call next before you call decode. And if next returns "false", then the cursor has been exhausted and there is nothing that can be decoded (and calling decode will return EOF).

Here is an example of misuse: https://gist.github.com/prestonvasquez/91392ba8a8228bad7e47764770cf351f
Here is an example of correct use: https://gist.github.com/prestonvasquez/9c4bfc8ab2bdb89f0342f4f49215c104

If the issue persists, could you provide us with a the version of the Go Driver and server version you are using? Could you also extend the code you've provided to illustrate how you are decoding data from the cursor?

Comment by Teresa Hale • Diamond Head Queen [ 20/Sep/23 ]

I'm facing the same issue.

 

sess := utils.EDB(c).Collection("stories")
 
	cond := bson.M{"_id": id}
 
	matchStage := bson.D{{Key: "$match", Value: cond}}
	ajoin := bson.D{{Key: "$lookup", Value: bson.M{
		"from":         "users",
		"localField":   "author",
		"foreignField": "_id",
		"as":           "author",
	}},
	}
	cunwind := bson.D{{"$unwind", "$chapters"}}
 
	cjoin := bson.D{{Key: "$lookup", Value: bson.M{
		"from":         "bands",
		"localField":   "chapters.bands",
		"foreignField": "_id",
		"as":           "chapters.bands",
	}}}
 
	cgroup := bson.D{{"$group",
		bson.M{"_id": "$_id",
			"doc": bson.M{
				"$first": "$$ROOT",
			},
			"chapters": bson.M{
				"$push": "$chapters",
			},
		}}}
	mobj := bson.D{{"$replaceRoot", bson.M{
		"newRoot": bson.M{
			"$mergeObjects": bson.A{
				"$doc",
				bson.M{"chapters": "$chapters"},
			},
		},
	},
	}}
	fjoin := bson.D{{Key: "$lookup", Value: bson.M{
		"from":         "ficmas_wishes",
		"localField":   "ficmas",
		"foreignField": "_id",
		"as":           "ficmas",
	}}}
	funwind := bson.D{{Key: "$unwind", Value: bson.M{
		"path": "$ficmas",
	},
	}}
	fwjoin := bson.D{{
		"$lookup", bson.M{
			"from":         "users",
			"localField":   "ficmas.wisher",
			"foreignField": "_id",
			"as":           "ficmas.wisher",
		},
	}}
	aunwind := bson.D{{Key: "$unwind", Value: bson.M{
		"path": "$author",
	},
	}}
	fwunwind := bson.D{{Key: "$unwind", Value: bson.M{
		"path": "$ficmas.wisher",
	},
	}}
	fbunwind := bson.D{{
		"$unwind", "$ficmas.bands",
	}}
 
	fbjoin := bson.D{{
		"$lookup", bson.M{
			"from":         "bands",
			"localField":   "ficmas.bands",
			"foreignField": "_id",
			"as":           "ficmas.bands",
		},
	}}
	fgroup := bson.D{{
		"$group",
		bson.M{"_id": "$_id",
			"doc": bson.M{
				"$first": "$$ROOT",
			},
			"oficmas": bson.M{
				"$first": "$ficmas",
			},
			"ficmasbands": bson.M{
				"$push": "$ficmas.bands",
			},
		}}}
	addo := bson.D{{
		"$addFields",
		bson.M{
			"ficmas.bands": "$ficmasbands",
		},
	}}
	unset := bson.D{{"$unset", bson.A{"ficmasbands", "oficmas"}}}
	lrr := bson.D{{
		"$replaceRoot", bson.M{
			"newRoot": bson.M{
				"$mergeObjects": bson.A{"$doc", bson.M{"_id": "$_id"}},
			},
		},
	}}
	pip := mongo.Pipeline{
		matchStage, ajoin, cunwind, cjoin, cgroup,
		mobj, fjoin, funwind, aunwind,
		fwjoin, fwunwind,
		fbunwind, fbjoin, fgroup, addo, unset, lrr,
	}
	curs, err := sess.Aggregate(context.TODO(), pip)
	if !utils.CheckErr(c, err) {
		return nil
	}

Regardless of whether I call `cursor.Next()` or not, I still get the error on decoding. Fix your shit please.

Comment by PM Bot [ 16/Nov/22 ]

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 Preston Vasquez [ 01/Nov/22 ]

ja7ad@live.com From the documentation on mongo.Cursor

Decode will unmarshal the current document into val and return any errors from the unmarshalling process without any modification. If val is nil or is a typed nil, an error will be returned.

It looks like you are trying to decode data from an empty cursor (i.e. cursor.Current == nil), try calling cursor.Next before cursor.Decode. Here is an example from the README.md:

ctx, cancel = context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
cur, err := collection.Find(ctx, bson.D{})
if err != nil { log.Fatal(err) }
defer cur.Close(ctx)
for cur.Next(ctx) {
    var result bson.D
    err := cur.Decode(&result)
    if err != nil { log.Fatal(err) }
    // do something with result....
}
if err := cur.Err(); err != nil {
    log.Fatal(err)
}

Comment by Esha Bhargava [ 28/Oct/22 ]

ja7ad@live.com Thanks for reporting this issue! We'll look into it and get back to you soon.

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