[GODRIVER-1452] Issue in filtering based on subdocument Created: 26/Dec/19  Updated: 08/Jan/20  Resolved: 08/Jan/20

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

Type: Bug Priority: Major - P3
Reporter: Abhay Kumar Assignee: Isabella Siu (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows



 Description   

For the document below

{ "_id":ObjectId("5e00841f03d2a48f209b0905"), "uID":\{"ABC"}

,

"units":[{"locID":"11","size":"small","accessible":true,"enabled":true,"reserved":false,"occupied":false},
{"locID":"13","size":"medium","accessible":true,"enabled":true,"reserved":false,"occupied":false},
{"locID":"15","size":"large","accessible":true,"enabled":true,"reserved":false,"occupied":false}]
}

when I try to filter based on uID and units.locID I'm getting any documents

Here is the filter and option

 
filter: = bson.D{{"$and", bson.A{bson.D{{"uID", "ABC"}, {"units.locID", "11"}}}}}
opts := options.FindOne().SetProjection(bson.D{

{"_id", 0}

, {"units", 1}})
 

I've tried this which gives back all units

filter: = bson.D{

{"uID", "ABC"}

, {"units.locID", "11"}} 

I want to get the unit which has  uID = ABC and locID = 11

 

 



 Comments   
Comment by Isabella Siu (Inactive) [ 08/Jan/20 ]

Hi abhaykumar1@gmail.com,

For what you are doing, you should use an aggregation pipeline:

pipeline := bson.A{
 bson.D{{"$match", bson.D{{"uID", "ABC"}}}},
 bson.D{{"$unwind", "$units"}},
 bson.D{{"$match", bson.D{{"units.locID", "11"}}}},
}
 
cur, err := coll.Aggregate(context.Background(),pipeline)

 

Please note that the GODRIVER project is for reporting bug or feature suggestions for the driver. For MongoDB-related support discussion please post on the mongodb-user group or Stack Overflow with the mongodb tag. A question like this involving more discussion would be best posted on the mongodb-users group.

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