Skip to content

Aggregate return empty #91

Description

@uslualper

I want to do what you see using the mgm library, but I couldn't figure it out.

[{
    '$unwind': {
        'path': '$nodes', 
        'includeArrayIndex': 'arrayIndex', 
        'preserveNullAndEmptyArrays': True
    }
}, {
    '$lookup': {
        'from': 'documents', 
        'localField': 'nodes.data.video', 
        'foreignField': '_id', 
        'as': 'results'
    }
}, {
    '$match': {
        '_id': ObjectId('6560f9e87c9e42e241e29c62')
    }
}]
func (w *Widget) Get(id string) (interface{}, error) {
widget := &mongodb.Widget{}
documentCollName := mgm.Coll(&mongodb.Document{}).Name()

if cur, err := mgm.Coll(widget).Aggregate(mgm.Ctx(), bson.A{
    bson.M{operator.Match: bson.M{field.ID: id}},// If I add this, no result is returned
    builder.S(builder.Unwind("$nodes", "arrayIndex", true)),
    builder.S(builder.Lookup(documentCollName, "nodes.data.video", "_id", "documents")),
}); err == nil {
    results := []bson.M{}
    cur.All(mgm.Ctx(), &results)
    return results, nil
} else {
    fmt.Println(err)
}

return widget, nil }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions