doute3621 2017-09-04 11:21
浏览 58
已采纳

Golang,mongodb不能选择

In my case I have a collection where I have stored a data with a structure that below

{ 
    "_id" : ObjectId("59ad187a0447d3617fb802b8"), 
    "fid" : ObjectId("59ad187a6b9600120bd03a53"), 
    "pr" : [
        {
            "_id" : ObjectId("59ad187a6b9600120bd03a53"), 
            "trashed" : false
        }
    ], 
    "ch" : [
        {
            "_id" : ObjectId("59ad18a36b9600120bd03a57"), 
            "trashed" : false
        }, 
        {
            "_id" : ObjectId("59ad18a36b9600120bd03a99"), 
            "trashed" : false
        }, 
        {
            "_id" : ObjectId("59ad18a36b9600120bd03a98"), 
            "trashed" : true
        }, 
        {
            "_id" : ObjectId("59ad18a36b9600120bd03a97"), 
            "trashed" : false
        }
    ]
}

So I want to get all objects in ch where trashed is false

Here is my query

       type ChildParentsData struct {
       Id      bson.ObjectId `json:"_id,omitempty" bson:"_id,omitempty"`
       Trashed bool          `json:"trashed" bson:"trashed"`
       }
        var tree []ChildParentsData
        err := Connection.Session.DB("cctv_storage").C("tree").Find(
               bson.M{
                  "fid": bson.ObjectIdHex(id), "ch.trashed": false
               }).Select(
               bson.M{
                  "ch.$": 1
                }).All(&tree)

But as a response I am getting all data, but I need only objects in ch

  • 写回答

1条回答 默认 最新

  • duanji1924 2017-09-04 12:23
    关注

    You can achieve this using the aggregation framework, thanks to the $replaceRoot operator introduced in MongoDB 3.4

    We first get matching documents for the specific fid, then we unwind the array and remove docmuments where ch.trashed is true. Finally, we remove the ch field by promoting the content of ch as root of the document

    Here is the code to achieve this:

    type ChildParentsData struct {
            Id      bson.ObjectId `json:"_id,omitempty" bson:"_id,omitempty"`
            Trashed bool          `json:"trashed" bson:"trashed"`
    }
    var tree []ChildParentsData
    
    pipeline := []bson.M{
        {"$match": bson.M{"fid": bson.ObjectIdHex("59ad187a6b9600120bd03a53")}},
        {"$unwind": "$ch"},
        {"$match": bson.M{"ch.trashed": false}},
        {"$replaceRoot": bson.M{"newRoot": "$ch"}}}
    
    err = Connection.Session.DB("cctv_storage").C("tree").Pipe(pipeline).All(&tree)
    
    if err != nil {
        fmt.Printf("error: %v", err)
        os.Exit(0)
    }
    fmt.Printf("doc: %v", tree)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Arduino电机和openmv连接异常
  • ¥15 Arcgis河网分级报错
  • ¥200 java+appium2.1+idea
  • ¥20 请帮我做一个EXE的去重TXT文本
  • ¥15 工价表引用工艺路线,应如何制作py和xml文件
  • ¥15 根据历史数据,推荐问题类型
  • ¥15 需要仿真图,简单的二阶系统实例
  • ¥15 stm32光控照明仿真
  • ¥15 使用人工智能的方法生成满足一定统计参数要求的随机数序列
  • ¥15 SENT协议中相关问题咨询