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

标签 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,
}

我想选择以下键值:

这是我随身携带的这些 key 的格式:

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]

感谢您的帮助。

最佳答案

您所需的字段在 map[interface{}]interface{} 中作为列出,这不符合 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)

关于mongodb - 使用golang从mongodb集合中获取特定的键值对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52621988/

相关文章:

mongodb - 在 mongodb 中结合不同的投影

go - 更新 MS SQL 表

java - 当条目超过饱和级别时,Java 8 HashMap 中如何创建树

java - 何时在 lucene 中进行索引

mongodb - 带有Nosql数据库的dart身份验证系统

mongodb - 在 ObjectId 字段上的两个集合之间进行 $lookup 查询

GoLang - 迭代数据以解码多个 YAML 结构

go - 初始化嵌套匿名结构

java - servlet的service方法中的HashMap

java - 如何在 Android 中按值对 HashMap<String , Byte> 进行排序?