duanhui7329 2018-08-07 15:07
浏览 185
已采纳

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

I am working on a mongo query in golang using mgo to query a child element to return the entire element

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

The query I am using in below bson.M{"_id": 0, "children": bson.M{"$elemMatch": {serialNo: 'abc123'}}}

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

Below is the find function

    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 struct

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

The above query works fine but returns only children element as below

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

I am not getting what is wrong with the query.

  • 写回答

1条回答 默认 最新

  • dongpai2468 2018-08-07 15:37
    关注

    $elemMatch exists as both a query and a projection. A query is used to actually filter which documents are returned, and a projection determines which part of the documents returned are shown. To reiterate: Projection doesn't filter which documents are returned, it limits which values per document are returned (similar to the SELECT part of SQL).

    mgo's Find function is the query, and Select is the projection. Therefore, you want your final code to look closer to this:

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

    With how you have your code set up, this is how it would look.

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

    However, I'd strongly advise you rename the fields in MongoSpec (dropping it and the Find function altogether might not be a bad idea, too). You use Query as your projection (the .Select() function) and you use Selector as your query (.Find()). That may be why you made this mistake in the first place.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能