[GODRIVER-1116] Slice projection not working as expected Created: 07/Jun/19  Updated: 01/Jul/19  Resolved: 01/Jul/19

Status: Closed
Project: Go Driver
Component/s: Core API, CRUD
Affects Version/s: 1.0.3
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Anindya Chatterjee Assignee: Run Tian Yu (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Ubuntu: 19.04
Go : 1.12.1 linux/amd64



 Description   

$slice projection is not working as expected in the go driver. I am using below code to fetch 5 elements from the nested array.

 

 

collection := db.Collection("products")
skip := (page - 1) * size
opt := options.FindOptions {
 Limit: &size,
 Skip: &skip,
 Projection: bson.D {{"products", "{ $slice: 5 }"}, {"name", ""}},
}
 
cur, err := collection.Find(c, bson.D{}, &opt)

 

The above code is returning all elements of product array instead of only 5.

 



 Comments   
Comment by Run Tian Yu (Inactive) [ 11/Jun/19 ]

Hi anidotnet, as per https://docs.mongodb.com/manual/reference/operator/projection/slice/, Projection should be formatted as https://github.com/mongodb/mongo-go-driver/blob/master/examples/documentation_examples/examples.go#L1303

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