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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘