mongodb - mongo aggregation query in golang with mgo driver -


i have following query in mongodb -

db.devices.aggregate({ $match: {userid: "v73tuqqzykbxfxswo", state: true}}, {   $project: {     userid: 1,     categoryslug: 1,     weight: {        $cond: [          {"$or": [             {$eq: ["$categoryslug", "air_fryer"] },            {$eq: ["$categoryslug", "iron"] }          ] },        0, 1] }      } },       {$sort: {weight: 1}},     { $limit : 10 } ); 

i'm trying write in golang using mgo driver not able wrap head around @ all!

how translate golang mgo query?

the examples on docs sufficient started. however, if not familiar golang, $cond part bit tricky. see below example code:

    collection := session.db("dbname").c("devices")      stage_match := bson.m{"$match":bson.m{"userid":"v73tuqqzykbxfxswo", "state": true}}      condition_weight := []interface{}{bson.m{"$or": []bson.m{                        bson.m{"$eq": []string{"$categoryslug", "air_fryer"}},                        bson.m{"$eq": []string{"$categoryslug", "iron"}},     }}, 0, 1}      stage_project:= bson.m{"$project": bson.m{"userid":1, "categoryslug":1, "weight": condition_weight}}      stage_sort := bson.m{"$sort": bson.m{"weight":1}}      stage_limit := bson.m{"$limit": 10}      pipe := collection.pipe([]bson.m{stage_match, stage_project, stage_sort, stage_limit}) 

see mgo: type pipe


Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -