简体   繁体   English

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

[英]How can I use mongodb projections with Go and mgo?

I am currently trying to extract a single object within a document array inside of mongodb. 我目前正在尝试在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: 我可以使用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: 我无法使用mgo做同样的事情,并尝试了以下方法:

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. 我使用httprouter和p.ByName(“...”)调用是传递给处理程序的参数。

Thanks in advance. 提前致谢。

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: 将使用Select方法,因为doc指出这可以选择应该为找到的结果检索哪些字段,因此使用$elemMatch运算符的投影可以与Select一起应用,最终查询类似于:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 mgo 和 Go 查询具有日期范围的 MongoDB? - How can I query MongoDB with date range using mgo and Go? 如何在 MongoDB 中同时使用两个投影? - How can I use two projections in MongoDB at the same time? Go中的MongoDB(golang)与mgo:如何使用逻辑运算符进行查询? - MongoDB in Go (golang) with mgo: how to use logical operators to query? 如何在Golang / mgo中的Mongodb中插入子文档? - How would I go about inserting a subdocument in Mongodb in Golang / mgo? Golang / mgo:我怎样才能让MongoDB在现场使用当前时间? - Golang/mgo: How can I ask MongoDB to use current time in a field? Golang/mgo:如何在 mongodb 中存储日期(不是 ISODate)? - Golang/mgo : How can I store Date (not ISODate) in mongodb? 如何在mgo(Go)中使用接口类型作为模型? - How to use interface type as a model in mgo (Go)? 如何使用$ push和$ each与go mgo驱动程序? - how to use $push and $each with the go mgo driver? 如何使用连接池将mgo会话转换为mongo-go-driver客户端? - How can I convert my mgo sessions to mongo-go-driver clients using connection pooling? MongoDB in Go (golang) with mgo:如何更新记录,确定更新是否成功并在单个原子操作中获取数据? - MongoDB in Go (golang) with mgo: How do I update a record, find out if update was successful and get the data in a single atomic operation?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM