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

时间:2018-10-03 07:34:06

标签: mongodb go hashmap

我想通过golang动态地从mongodb集合中获取特定的键值对。

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

我该怎么做,但它必须是动态的,因为选择键值对在我这边正在发生变化:

托收文件:

{
    "_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,
}

我要选择以下键值:

这些密钥随我一起使用的格式:

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]

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

您所需的字段在map[interface{}]interface{}中以 values 的形式列出,该值不符合Query.Select()期望的规范。描述字段的最常见类型是bson.M,其中键应为要检索的字段的名称。

因此您必须构建例如键中的bson.M值,并将其与Query.Select()一起使用。为此,我们必须找到一种将值从interface{}类型转换为string的方法。最简单,最方便的方法是为此使用fmt.Sprint()

这是一个如何执行此操作的示例:

// 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)