草庐IT

go - 从集合中选择嵌套字段

coder 2023-07-01 原文

我正在尝试从 Mongo 集合中的嵌套对象中获取单个字段。

我需要获取所有符合条件的标签。我能够查询并获取整个对象,但无法获取标签列表或数组。

// Content struct
type Content struct {
    ID                  bson.ObjectId           `json:"id" bson:"_id"`
    PrimaryMarket       string                  `json:"primary_market" bson:"primary_market"`
    Title               string                  `json:"title" bson:"title"`
    Description         string                  `json:"description" bson:"description"`
    Owner               User                    `json:"owner" bson:"owner"`
    IsActive            bool                    `json:"is_active" bson:"is_active"`
    File                string                  `json:"file" bson:"file"`
    FileName            string                  `json:"file_name" bson:"file_name"`
    FileType            string                  `json:"file_type" bson:"file_type"`
    FileSize            string                  `json:"file_size" bson:"file_size"`
    FileExt             string                  `json:"file_ext" bson:"file_ext"`
    OriginalHeight      int                     `json:"original_height" bson:"original_height"`
    OriginalWidth       int                     `json:"original_width" bson:"original_width"`
    Height              int                     `json:"height" bson:"height"`
    Width               int                     `json:"width" bson:"width"`
    Tags                []Tag                   `json:"tags" bson:"tags"`
    Flags               []Flagged               `json:"flags" bson:"flags"`
    CreatedDate         time.Time               `json:"created_date" bson:"created_date"`
}

// Tag struct
type Tag struct {
    ID                  bson.ObjectId           `json:"id" bson:"_id"`
    Tag                 string                  `json:"tag" bson:"tag"`
    Market              string                  `json:"market" bson:"market"`
    CreatedBy           User                    `json:"created_by" bson:"created_by"`
    CreatedDate         time.Time               `json:"created_date" bson:"created_date"`
    IsActive            bool                    `json:"is_active" bson:"is_active"`
}

我的查询

var result []struct {
    Tags []struct {
        Description string `bson:"description"`
    } `bson:"tags"`
}

find := app.Session.DB("mydb").C("content").Find(bson.M{"primary_market": "Photos", "tags.description": &bson.RegEx{Pattern: query, Options: "i"}}).All(&result)

我试过使用点符号的 Select 语句,但这似乎不起作用。任何帮助将不胜感激。

谢谢

最佳答案

MongoDB 将为上述查询生成一个对象数组,如下所示:

{ "_id" : ObjectId("5a5c401ddbff3fdea15082ee"), "tags" : [ { "tag" : "query" }, { "tag" : "something" } ] }
{ "_id" : ObjectId("5a5c423ddbff3fdea15082f0"), "tags" : [ { "tag" : "something" }, { "tag" : "query" } ] }

因此,为了正确解码这个对象数组,result 变量应该如下所示:

var result []struct {
    Tags []struct {
        Tag string `bson:"tag"`
    } `bson:"tags"`
}

关于go - 从集合中选择嵌套字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48254402/

有关go - 从集合中选择嵌套字段的更多相关文章

  1. ruby-on-rails - Rails 编辑表单不显示嵌套项 - 2

    我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib

  2. ruby - 将散列转换为嵌套散列 - 2

    这道题是thisquestion的逆题.给定一个散列,每个键都有一个数组,例如{[:a,:b,:c]=>1,[:a,:b,:d]=>2,[:a,:e]=>3,[:f]=>4,}将其转换为嵌套哈希的最佳方法是什么{:a=>{:b=>{:c=>1,:d=>2},:e=>3,},:f=>4,} 最佳答案 这是一个迭代的解决方案,递归的解决方案留给读者作为练习:defconvert(h={})ret={}h.eachdo|k,v|node=retk[0..-2].each{|x|node[x]||={};node=node[x]}node[

  3. ruby-on-rails - 如何验证非模型(甚至非对象)字段 - 2

    我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss

  4. ruby-on-rails - form_for 中不在模型中的自定义字段 - 2

    我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢

  5. ruby-on-rails - 在 Rails 和 ActiveRecord 中查询时忽略某些字段 - 2

    我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr

  6. Ruby——嵌套类和子类是一回事吗? - 2

    下面例子中的Nested和Child有什么区别?是否只是同一事物的不同语法?classParentclassNested...endendclassChild 最佳答案 不,它们是不同的。嵌套:Computer之外的“Processor”类只能作为Computer::Processor访问。嵌套为内部类(namespace)提供上下文。对于ruby​​解释器Computer和Computer::Processor只是两个独立的类。classComputerclassProcessor#Tocreateanobjectforthisc

  7. ruby - 模块嵌套代码风格偏好 - 2

    我的假设是moduleAmoduleBendend和moduleA::Bend是一样的。我能够从thisblog找到解决方案,thisSOthread和andthisSOthread.为什么以及什么时候应该更喜欢紧凑语法A::B而不是另一个,因为它显然有一个缺点?我有一种直觉,它可能与性能有关,因为在更多命名空间中查找常量需要更多计算。但是我无法通过对普通类进行基准测试来验证这一点。 最佳答案 这两种写作方法经常被混淆。首先要说的是,据我所知,没有可衡量的性能差异。(在下面的书面示例中不断查找)最明显的区别,可能也是最著名的,是你的

  8. ruby-on-rails - 使用回形针的嵌套形式 - 2

    我有一个名为posts的模型,它有很多附件。附件模型使用回形针。我制作了一个用于创建附件的独立模型,效果很好,这是此处说明的View(https://github.com/thoughtbot/paperclip):@attachment,:html=>{:multipart=>true}do|form|%>posts中的嵌套表单如下所示:prohibitedthispostfrombeingsaved:@attachment,:html=>{:multipart=>true}do|at_form|%>附件记录已创建,但它是空的。文件未上传。同时,帖子已成功创建...有什么想法吗?

  9. ruby-on-rails - Rails 3,嵌套资源,没有路由匹配 [PUT] - 2

    我真的为这个而疯狂。我一直在搜索答案并尝试我找到的所有内容,包括相关问题和stackoverflow上的答案,但仍然无法正常工作。我正在使用嵌套资源,但无法使表单正常工作。我总是遇到错误,例如没有路线匹配[PUT]"/galleries/1/photos"表格在这里:/galleries/1/photos/1/edit路线.rbresources:galleriesdoresources:photosendresources:galleriesresources:photos照片Controller.rbdefnew@gallery=Gallery.find(params[:galle

  10. postman——集合——执行集合——测试脚本——pm对象简单示例02 - 2

    //1.验证返回状态码是否是200pm.test("Statuscodeis200",function(){pm.response.to.have.status(200);});//2.验证返回body内是否含有某个值pm.test("Bodymatchesstring",function(){pm.expect(pm.response.text()).to.include("string_you_want_to_search");});//3.验证某个返回值是否是100pm.test("Yourtestname",function(){varjsonData=pm.response.json

随机推荐