dswsl2016 2018-10-03 07:34
浏览 447
已采纳

使用golang从mongodb集合中获取特定的键值对

I want to fetch specific key value pairs from mongodb collection with golang dynamically.

err := collection.Find(operations).Limit(2).All(&products)

How I can do it but it must be dynamically, because the select key value pairs are changing on my side:

Collection document:

{
    "_id" : 168,
    "entity_id" : "168",
    "type_id" : "simple",
    "attribute_set_id" : "24",
    "entity_type_id" : "4",
    "created_at" : "2013-10-31 14:51:18",
    "has_options" : "0",
    "required_options" : "0",
    "sku" : "AIC-19000-16",
    "updated_at" : "2016-11-22 21:04:46",
    "base_type" : 154,
    "table_shape" : 164,
    "manufacturer" : 15,
    "delivery" : "Free Delivery & Setup",
    "visibility" : 4,
    "tax_class_id" : 2,
    "status" : 1,
    "enable_googlecheckout" : 1,
    "discontinued" : 0,
    "featured_fme" : 0,
    "featured_product" : 0,
    "amxnotif_hide_alert" : 1,
    "is_recurring" : 0,
    "condition" : 3043,
    "ships" : null,
    "ignore_wisepricer" : 0,
    "fedexable" : null,
    "dropshipped" : 0,
    "verified_by" : 3301,
    "reward_point_product" : null,
    "mw_reward_point_sell_product" : null,
    "ashley_sale" : 0,
    "disable_amazonpayments" : 0,
    "for_configurables" : null,
    "rfm" : 0,
    "mk_stockmessage" : false,
    "mk_hideshipdate" : 0,
    "reviews_counter" : 0,
    "mpn" : "19000-16",
    "name" : "After Eight Titanium Leg Rectangular Dining Table",
    "style" : "73",
    "furniture_type" : "76",
    "meta_title" : "After Eight Titanium Leg Rectangular Dining Table, 
    19000-16, Aico Furniture",
    "meta_description" : "After Eight Titanium Leg Rectangular Dining 
    Table from Aico Furniture, 19003-88",
    "options_container" : "container2",
    "url_key" : "after-eight-titanium-leg-rectangular-dining-table",
    "url_path" : "after-eight-titanium-leg-rectangular-dining- 
    table.htm",
    "gallery" : "/9/19000-16_d1.jpg, /9/19000-16_d2.jpg, /9/19000- 
    16_d3.jpg, /9/19000-16_d4.jpg, /9/19000-16_d5.jpg, /9/19000- 
    16_d6.jpg, /9/19000-16_d7.jpg,",
    "image" : "/a/f/after8-silo-4legrecdin-sm-19000-16.jpg",
    "small_image" : "/a/f/after8-silo-4legrecdin-sm-19000-16.jpg",
    "thumbnail" : "/a/f/after8-silo-4legrecdin-sm-19000-16.jpg",
    "image_label" : null,
    "small_image_label" : null,
    "thumbnail_label" : null,
    "table_height" : "2640,2642",
    "dining_type" : "2645",
    "product_listing_ads" : "Aico Furniture",
    "carton_dimensions" : null,
    "msrp_enabled" : "2",
    "msrp_display_actual_price_type" : "4",
    "set_includes" : null,
    "custom_design" : null,
    "page_layout" : null,
    "gift_message_available" : null,
    "color" : "3553",
    "clearance" : null,
    "rfm_type" : null,
    "mk_stocktext" : null,
    "mk_ships_in" : null,
    "mk_preordertext" : null,
    "jet_product_status" : "not_uploaded",
    "meta_keyword" : "After Eight Titanium Leg Rectangular Dining 
    Table, 19000-16, Aico Furniture, dining room, dining, dining room 
    furniture, Leg Rectangular Dining Table, After Eight Titanium, 
    Modern, Wood, Light Colors, amini furniture, michael amini, 
    amini,",
    "description" : "",
    "short_description" : "",
    "features" : "Part of After Eight Collection <br />Titanium Finish 
    <br />Hollywood Regency Style <br /> Bold tonal contrasts and fun 
    geometric shapes <br />Design that blends the best of many styles 
    <br />Uniquely customizable look <br />Bold splashes of color 
    &amp; striking patterned accessories <br />Includes 2- 24\" 
    Extension Leaves <br />Table Extends from 74\" to 122\" <br 
    />Mirrored glass inserts in the top <br />Platinum accents <br 
    />Also available in Pearl <br /> Optional Chairs<br />Table Only, 
    Chairs Sold Separately<br />",
    "dimensions" : "4 Leg Rectangular Dining Table: 127.50\"W x 45\"D 
    x 31\"H - 290lbs. <br />Optional Side Chair: 21.50\"W x 26\"D x 
    45.75\"H - 24lbs. <br />Optional Arm Chair: 23.75\"W x 26\"D x 
    45.75\"H - 28lbs. <br />",
    "custom_layout_update" : null,
    "upc" : null,
    "cost" : 999.0,
    "price" : 2849.0,
    "tier_price_for_bundle" : 2279.2,
    "weight" : 290.0,
    "regularprice" : 2629.0,
    "special_price" : null,
    "msrp" : null,
    "estimated_shipping" : null,
    "estimated_set_shipping" : null,
    "family_rating_summary" : 0.0,
    "news_from_date" : null,
    "news_to_date" : null,
    "special_from_date" : null,
    "special_to_date" : null,
    "custom_design_from" : null,
    "custom_design_to" : null,
    "mk_expecdate" : null,
    "mk_preorderdate" : null,
}

I want to select below key values:

This is the format in which these keys come with me:

map[2: 3:manufacturer 8:upc 12:weight 15: 0:name 5:short_description 
6:sku 13:category 1: 4:manufacturer 9:image 10:url 
16:product_listing_ads 7:mpn 11:final_price 14:mapping:1]

Thanks for your help.

  • 写回答

1条回答 默认 最新

  • dsa456369 2018-10-03 08:05
    关注

    Your desired fields are listed in a map[interface{}]interface{} as values which is not conform to the spec expected by Query.Select(). The most common type to describe the fields is bson.M, where the keys should be the names of the fields to retrieve.

    So you have to construct e.g. a bson.M value from your keys, and use that with Query.Select(). For this we have to find a way to convert the values from interface{} type to string. Simplest and most convenient way is to use fmt.Sprint() for this.

    This is an example how you can do that:

    // Example input:
    fields := map[interface{}]interface{}{
        3: "name",
        1: "manufacturer",
        9: "sku",
    }
    
    fields2 := bson.M{}
    for _, name := range fields {
        fields2[fmt.Sprint(name)] = 1
    }
    
    err := collection.Find(operations).Select(fields2).Limit(2).All(&products)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于模型训练的一个问题
  • ¥15 装了几千台服务器从来没遇到这种问题,哎看下哪位帮我解决吧
  • ¥15 单片机程序上的困难问题
  • ¥15 请教某软件缓存Ts文件破解合并mp4的方法
  • ¥15 求小游戏炸弹人中关于敌人的C++代码
  • ¥15 拿到这个服务器最高权限有偿
  • ¥50 来个抓app跳转支付宝转链接的
  • ¥15 remotes安装提示没有description文件
  • ¥15 AttributeError: 'NoneType' object has no attribute 'drop_duplicates'报错
  • ¥15 以下代码,运行结果报错