使用 golang bson 从 mongo 获取整个元素的问题,而不是仅返回子元素

Issue getting entire element from mongo using golang bson instead only child element is being returned

我正在 golang 中使用 mgo 查询子元素 return 整个元素 mongo 查询

{
    "_id" : ObjectId("5b64a0d3931653c36bcaf0b5"),
    "quantity" : 2,
    "product" : "ABC",   
    "children" : [ 
        {           
            "isBlocked" : true,
            "blockedMessage" : "Error occurred: TRACEID",
            "serialNo" : "abc123",
            "token" : "foo456",            
        }
    ]
}

我在下面使用的查询 bson.M{"_id": 0, "children": bson.M{"$elemMatch": {序列号: 'abc123'}}}

Find(MongoSpec{Selector: bson.M{}, Query: bson.M{"_id": 0, "children": bson.M{"$elemMatch": fields}}})

下面是查找函数

    documents := []interface{}{}
        s := spec.(MongoSpec).Selector
        q := spec.(MongoSpec).Query
        query := session.
            DB(repo.Config.DatabaseName).
            C(repo.CollectionName).
            Find(s)

        if q != nil {
            query = query.Select(q)
        }

        err := query.All(&documents)

MongoSpec 结构

  type MongoSpec struct {
        Selector interface{}
        Query    interface{}
    }

上面的查询工作正常,但是 return 只有下面的子元素

"children" : [ 
            {           
                "isBlocked" : true,
                "blockedMessage" : "Error occurred: TRACEID",
                "serialNo" : "abc123",
                "token" : "foo456",            
            }
        ]

我没明白查询有什么问题。

$elemMatch 既存在 query and a projection 查询用于实际过滤 哪些文档返回,投影确定 显示返回的文档的哪一部分。重申一下:Projection 不会过滤返回哪些文档,它会限制每个文档返回哪些值(类似于 SQL 的 SELECT 部分)。

mgo的Find函数是查询,Select是投影。因此,您希望最终代码看起来更接近于此:

c.Find(
    bson.M{
        "children": bson.M{
            "$elemMatch": bson.M{serialNo: "abc123"},
        },
    },
).Select(
    bson.M{
        "_id": 0,
    },
)

根据您设置代码的方式,这就是它的外观。

Find(
    MongoSpec{
        Selector: bson.M{"children": bson.M{"$elemMatch": fields}},
        Query: bson.M{"_id": 0},
    },
)

但是,我强烈建议您重命名 MongoSpec 中的字段(将其和 Find 函数一起删除也不是一个坏主意)。您使用 Query 作为投影(.Select() 函数)并使用 Selector 作为查询(.Find())。这可能就是你一开始犯这个错误的原因。