MongoDB find in collection with unknown key

谁说我不能喝 提交于 2019-12-01 07:28:09

问题


I need help with query to mongodb,

here my json, I need to find all items with "name_color": "8650AC", I can find one

db.items.find( { "rgDescriptions.230130277_253072421.name_color": "8650AC" } )

but don't know how to find all, because whey have different keys.

How to do that?

 {
  "success": true,
  "rgCurrency": [],
  "rgDescriptions": {
    "230130277_253072421": {
      "appid": "570",
      "classid": "230130277",
      "instanceid": "253072421",
      "icon_url": "Fq3N3-CjKwAJlvOA-3BW3q7hB26k0yVO3FJnn16RCy6QqOUm4btvB75OuSaAEAJDqfsAMfufZUOXXyaOQ4wPbpGh_yypv20ZrBT4TtRQDA-s4BR5-45kTM1IFp9egkQv36-yc6zpfRb6ArwmhgYJQ_-jEC37nzxOiF4qwgDSXCyNqux6quspWb0P6w==",
      "icon_url_large": "Jke-KvK7aqOnDVvaqvGl0Z4LdJu2y2TtcsnPxQ8Q-CGgQpbT86MupBDVEXzRkfFMmRFzxOmHJOA5xI7UEg38YaFLjNm7pyy6Ao9QFIXR_wCcCmeM6ZYl72PTvsUPA8Z_71Oe2_KgO7YGnEJzh42tGsIcZtK5h3q9cJTTkFYC_XXoRJuM66Nn5VTLEHPZkOhBkQ==",
      "icon_drag_url": "",
      "name": "Unusual Enduring War Dog",
      "market_hash_name": "Unusual Enduring War Dog",
      "market_name": "Unusual Enduring War Dog",
      "name_color": "8650AC",
      "background_color": "",
      "type": "\u041a\u0443\u0440\u044c\u0435\u0440, Mythical",
      "tradable": 1,
      "marketable": 1,
      "descriptions": [{
        "type": "html",
        "value": "\u0425\u043e\u0440\u043e\u0448\u043e \u044d\u043a\u0438\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043f\u0435\u0441, \u043d\u0435 \u043f\u043e\u043a\u0438\u0434\u0430\u044e\u0449\u0438\u0439 \u0445\u043e\u0437\u044f\u0438\u043d\u0430 \u0432 \u0441\u0440\u0430\u0436\u0435\u043d\u0438\u0438. \u042d\u0442\u043e\u0442 \u0437\u0432\u0435\u0440\u044c \u043f\u043e\u0432\u0438\u0434\u0430\u043b \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0443\u0436\u0430\u0441\u0430\u044e\u0449\u0438\u0445 \u0434\u0435\u044f\u043d\u0438\u0439 \u0438 \u0432\u0435\u043b\u0438\u043a\u0438\u0445 \u043f\u043e\u0431\u0435\u0434. \u0417\u0430 \u0435\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u044f\u043d\u043d\u043e\u0439 \u043c\u0430\u0441\u043a\u043e\u0439 \u0441\u043a\u0440\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0441\u0442\u043e\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u0430\u0432\u043e\u0441\u0443\u0434\u0438\u0435."
      }, {
        "type": "html",
        "value": "<div style=\"white-space: nowrap; margin: 10px\"><div style=\"white-space: nowrap; padding: 3px;\"><div style=\"width: 60px; height: 32px; vertical-align: top; display: inline-block;\"><div style=\"width: 48px; height: 32px; vertical-align: top; display: inline-block; border: 2px solid rgb(255, 255, 255);>\"><div style=\"position: absolute; width: 48px; height: 32px; vertical-align: top; display: inline-block; background-size: contain; background-image: url(http:\/\/cdn.dota2.com\/apps\/570\/icons\/econ\/sockets\/gem_effect.6be419ef6706ede2de7f6f27ab18748ae3017b60.png)\"><\/div><\/div><\/div><div style=\"vertical-align: top; display: inline-block; margin-left: 12px padding: 2px\"><span style=\"font-size: 18px; color: rgb(255, 255, 255)\">Bleak Hallucination<\/span><br><span style=\"font-size: 12px\">\u041f\u043e\u0442\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0438\u0439 \u0441\u0430\u043c\u043e\u0446\u0432\u0435\u0442<\/span><\/div><\/div><div style=\"white-space: nowrap; padding: 3px;\"><div style=\"width: 60px; height: 32px; vertical-align: top; display: inline-block;\"><div style=\"width: 48px; height: 32px; vertical-align: top; display: inline-block; border: 2px solid rgb(26, 61, 133);>\"><div style=\"position: absolute; width: 48px; height: 32px; vertical-align: top; display: inline-block; background-size: contain; background-image: url(http:\/\/cdn.dota2.com\/apps\/570\/icons\/econ\/sockets\/gem_color.5a9386e4e99cae2af84385fcb8eea1d0bcf92dcf.png)\"><\/div><div style=\"position: absolute; filter:alpha(opacity=90); -moz-opacity:0.9; -khtml-opacity: 0.9; opacity: 0.9; width: 48px; height: 32px; vertical-align: top; display: inline-block; background-size: contain; background-image: url(http:\/\/cdn.dota2.com\/apps\/570\/icons\/econ\/sockets\/gem_color_mask.c65f0f4e7c1687741863d1e1d675949afd889d5d.png)\"><\/div><\/div><\/div><div style=\"vertical-align: top; display: inline-block; margin-left: 12px padding: 2px\"><span style=\"font-size: 18px; color: rgb(26, 61, 133)\">\u0421\u0438\u043d\u0438\u0439 \u043a\u0440\u0438\u0441\u0442\u0430\u043b\u043b<\/span><br><span style=\"font-size: 12px\">\u041f\u0440\u0438\u0437\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0441\u0430\u043c\u043e\u0446\u0432\u0435\u0442<\/span><\/div><\/div><\/div>"
      }],
      "tags": [{
        "internal_name": "unusual",
        "name": "Unusual",
        "category": "Quality",
        "color": "8650AC",
        "category_name": "\u041a\u0430\u0447\u0435\u0441\u0442\u0432\u043e"
      }, {
        "internal_name": "Rarity_Mythical",
        "name": "Mythical",
        "category": "Rarity",
        "color": "8847ff",
        "category_name": "\u0420\u0435\u0434\u043a\u043e\u0441\u0442\u044c"
      }, {
        "internal_name": "DOTA_Custom_Courier",
        "name": "\u041a\u0443\u0440\u044c\u0435\u0440",
        "category": "Type",
        "category_name": "\u0422\u0438\u043f"
      }, {
        "internal_name": "courier",
        "name": "\u041a\u0443\u0440\u044c\u0435\u0440",
        "category": "Slot",
        "category_name": "\u0422\u0438\u043f"
      }, {
        "internal_name": "DOTA_OtherType",
        "name": "\u0414\u0440\u0443\u0433\u0438\u0435",
        "category": "Hero",
        "category_name": "\u0413\u0435\u0440\u043e\u0439"
      }, {
        "internal_name": "HeroID",
        "name": "1000",
        "category": "HeroID",
        "category_name": "HeroID"
      }]
    },
    "230130201_253034070": {
      "appid": "570",
      "classid": "230130201",
      "instanceid": "253034070",
      "icon_url": "7nco17KZIo5GcLtsZbVQLVY74mb26SzAk7Qvc8BUDd1ocgAus4FmifGo8coe1QSwUSHlOamlbM3YuW5i3UkJnWlzDif7hWTW4eixj0efH4xcLvJxqbQtxZa5OSOeElSOJSRWda6BJ8mwsvLLHJwP4g8sr3L4pzWSzuM1c51CW8E2fwg=",
      "icon_url_large": "CB0CFH5IyIHVJotMrABYsbBRyKU6OMbPAOIfUwnhBUGOGCrtf1CMhmL-werXYAwst0vP-mV0hsJL715CFPwBAY8ZJOQ3VI7Zcr6Br44qFxC6RNiyZWW2wAX-VlJIoAZEkU50tTFVmZAhs8zq3n9WKr4SiexhJ43NALgBVlfwAhbBTyfmaR-MmHY=",
      "icon_drag_url": "",
      "name": "Unusual Fearless Badger",
      "market_hash_name": "Unusual Fearless Badger",
      "market_name": "Unusual Fearless Badger",
      "name_color": "8650AC",
      "background_color": "",
      "type": "\u041a\u0443\u0440\u044c\u0435\u0440, Mythical",
      "tradable": 1,
      "marketable": 1,
      "descriptions": [{
        "type": "html",
        "value": "\u041f\u043e\u0436\u0430\u043b\u0443\u0439, \u043b\u0443\u0447\u0448\u0438\u043c \u043a\u0443\u0440\u044c\u0435\u0440\u043e\u043c \u0438\u0437 \u0432\u0441\u0435\u0445 \u043b\u0435\u0441\u043d\u044b\u0445 \u043e\u0431\u0438\u0442\u0430\u0442\u0435\u043b\u0435\u0439 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0437\u0432\u0430\u0442\u044c \u0431\u0430\u0440\u0441\u0443\u043a\u0430. \u0411\u0443\u0434\u0443\u0447\u0438 \u0431\u0435\u0441\u0441\u0442\u0440\u0430\u0448\u043d\u044b\u043c \u0438 \u0441\u0442\u0440\u0435\u043c\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u043c, \u043e\u043d \u0441\u0430\u043c\u043e\u043e\u0442\u0432\u0435\u0440\u0436\u0435\u043d\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442 \u043b\u044e\u0431\u043e\u0435 \u043f\u043e\u0440\u0443\u0447\u0435\u043d\u0438\u0435 \u0442\u043e\u0433\u043e, \u043a\u0442\u043e \u0437\u0430\u0441\u043b\u0443\u0436\u0438\u043b \u0435\u0433\u043e \u0434\u043e\u0432\u0435\u0440\u0438\u0435."
      }, {
        "type": "html",
        "value": "<div style=\"white-space: nowrap; margin: 10px\"><div style=\"white-space: nowrap; padding: 3px;\"><div style=\"width: 60px; height: 32px; vertical-align: top; display: inline-block;\"><div style=\"width: 48px; height: 32px; vertical-align: top; display: inline-block; border: 2px solid rgb(255, 255, 255);>\"><div style=\"position: absolute; width: 48px; height: 32px; vertical-align: top; display: inline-block; background-size: contain; background-image: url(http:\/\/cdn.dota2.com\/apps\/570\/icons\/econ\/sockets\/gem_effect.6be419ef6706ede2de7f6f27ab18748ae3017b60.png)\"><\/div><\/div><\/div><div style=\"vertical-align: top; display: inline-block; margin-left: 12px padding: 2px\"><span style=\"font-size: 18px; color: rgb(255, 255, 255)\">Trail of the Amanita<\/span><br><span style=\"font-size: 12px\">\u041f\u043e\u0442\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0438\u0439 \u0441\u0430\u043c\u043e\u0446\u0432\u0435\u0442<\/span><\/div><\/div><div style=\"white-space: nowrap; padding: 3px;\"><div style=\"width: 60px; height: 32px; vertical-align: top; display: inline-block;\"><div style=\"width: 48px; height: 32px; vertical-align: top; display: inline-block; border: 2px solid rgb(98, 110, 91);>\"><div style=\"position: absolute; width: 48px; height: 32px; vertical-align: top; display: inline-block; background-size: contain; background-image: url(http:\/\/cdn.dota2.com\/apps\/570\/icons\/econ\/sockets\/gem_color.5a9386e4e99cae2af84385fcb8eea1d0bcf92dcf.png)\"><\/div><div style=\"position: absolute; filter:alpha(opacity=90); -moz-opacity:0.9; -khtml-opacity: 0.9; opacity: 0.9; width: 48px; height: 32px; vertical-align: top; display: inline-block; background-size: contain; background-image: url(http:\/\/cdn.dota2.com\/apps\/570\/icons\/econ\/sockets\/gem_color_mask.c65f0f4e7c1687741863d1e1d675949afd889d5d.png)\"><\/div><\/div><\/div><div style=\"vertical-align: top; display: inline-block; margin-left: 12px padding: 2px\"><span style=\"font-size: 18px; color: rgb(98, 110, 91)\">\u0427\u0443\u043c\u043d\u043e\u0439 \u0441\u0435\u0440\u044b\u0439<\/span><br><span style=\"font-size: 12px\">\u041f\u0440\u0438\u0437\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0441\u0430\u043c\u043e\u0446\u0432\u0435\u0442<\/span><\/div><\/div><\/div>"
      }],
      "tags": [{
        "internal_name": "unusual",
        "name": "Unusual",
        "category": "Quality",
        "color": "8650AC",
        "category_name": "\u041a\u0430\u0447\u0435\u0441\u0442\u0432\u043e"
      }, {
        "internal_name": "Rarity_Mythical",
        "name": "Mythical",
        "category": "Rarity",
        "color": "8847ff",
        "category_name": "\u0420\u0435\u0434\u043a\u043e\u0441\u0442\u044c"
      }, {
        "internal_name": "DOTA_Custom_Courier",
        "name": "\u041a\u0443\u0440\u044c\u0435\u0440",
        "category": "Type",
        "category_name": "\u0422\u0438\u043f"
      }, {
        "internal_name": "courier",
        "name": "\u041a\u0443\u0440\u044c\u0435\u0440",
        "category": "Slot",
        "category_name": "\u0422\u0438\u043f"
      }, {
        "internal_name": "DOTA_OtherType",
        "name": "\u0414\u0440\u0443\u0433\u0438\u0435",
        "category": "Hero",
        "category_name": "\u0413\u0435\u0440\u043e\u0439"
      }, {
        "internal_name": "HeroID",
        "name": "1000",
        "category": "HeroID",
        "category_name": "HeroID"
      }]
    }
   }
  }

回答1:


You can try use "$where" clause:

db.items.find({"$where" : function(){ 
    for( var c in this ){
        if( c == "rgDescriptions" ){ 
            for(var i in this[c]){ 
                 for(var j in this[c][i]){
                      if(j == 'name_color' && this[c][i][j] == '8650AC'){
                           return true;
                      }
                 }
             }
        };
     }
     return false; 
}});

But warn you that this clause is 'slow' solution cause each document has to be converted from BSON to a Javascript object and then run through the "$where" expression.




回答2:


If your database design stays as presented in your example the only chance you have is the answer given by CodeGroover. To get a better performance I would create another field name_colors which is an array of all name_colour values of the sub-documents below rgDescriptions.

In your example:

name_colours: [ "8650AC", "8650AC" ] // both sub-documents had the same name_colour

Then you can get all documents containing a sub-document with the wanted name_colour:

db.coll.find( { name_colours: "8650AC" }, ... )

If you wish to know which sub-document made a document a hit, you can parse the documents' sub-documents client-side.




回答3:


first you have to parse the code like

params='enter here the above json data' params = JSON.parse(params)

params['rgDescriptions']['230130277_253072421']['name_color'] it will help you Njoy!



来源:https://stackoverflow.com/questions/21423954/mongodb-find-in-collection-with-unknown-key

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!