dongshi4589 2015-11-17 07:36
浏览 96
已采纳

如何在Go和mgo中使用mongodb投影?

I am currently trying to extract a single object within a document array inside of mongodb. This is a sample dataset:

"_id" : ObjectId("564aae61e0c4e5dddb07343b"),
"name" : "The Races",
"description" : "Horse races",
"capacity" : 0,
"open" : true,
"type" : 0,
"races" : [
    {
        "_id" : ObjectId("564ab9097628ba2c6ec54423"),
        "race" : {
            "distance" : 3000,
            "user" : {
                "_id" : ObjectId("5648bdbe7628ba189e011b18"),
                "status" : 1,
                "lastName" : "Miranda",
                "firstName" : "Aramys"
            }
        }
    },
    {
        "_id" : ObjectId("564ab9847628ba2c81f2f34a"),
        "bet" : {
            "distance" : 3000,
            "user" : {
                "_id" : ObjectId("5648bdbe7628ba189e011b18"),
                "status" : 1,
                "lastName" : "Miranda",
                "firstName" : "Aramys"
            }
        }
    },{...}
]

I can successfully query using the following in mongo:

db.tracks.find({"_id": ObjectId("564aae61e0c4e5dddb07343b")}, {"races": { $elemMatch: {"_id": ObjectId("564ab9847628ba2c81f2f34a")}}}).pretty()

I am unable to do the same using mgo and have tried the following:

Using nesting (Throws: missing type in composite literal, missing key in map literal)

// Using nesting (Throws: missing type in composite literal, missing key in map literal)
c.Find(bson.M{{"_id": bson.ObjectIdHex(p.ByName("id"))}, bson.M{"races": bson.M{"$elemMatch": bson.M{"_id": bson.ObjectIdHex(p.ByName("raceId"))}}}}).One(&result)

// Using select (Returns empty)
c.Find(bson.M{"_id": bson.ObjectIdHex(p.ByName("id"))}).Select(bson.M{"races._id": bson.ObjectIdHex(p.ByName("raceId"))}).One(&result)

//As an array (Returns empty)
c.Find([]bson.M{{"_id": bson.ObjectIdHex(p.ByName("id"))}, bson.M{"races": bson.M{"$elemMatch": bson.M{"_id": bson.ObjectIdHex(p.ByName("raceId"))}}}}).One(&result)

I am using httprouter and p.ByName("...") invocations are parameters passed to the handler.

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • duanbo19834 2015-11-17 10:56
    关注

    Would go with the Select method as the doc states that this enables selecting which fields should be retrieved for the results found, thus the projection using $elemMatch operator can be applied here in conjuction with Select, with your final query looking something like:

    c.Find(bson.M{
        "_id": bson.ObjectIdHex(p.ByName("id"))
    }).Select(bson.M{
        "races": bson.M{
            "$elemMatch": bson.M{
                "_id": bson.ObjectIdHex(p.ByName("raceId"))
            }
        }
    }).One(&result)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格