2015-09-28 130 views
1

我在蒙戈以下集合:

> db.styles.find({ "_id" : "EP01L"}).pretty(); 
{ 
    "_id" : "EP01L", 
    "__v" : 5, 
    "categoryIds" : [ 
     ObjectId("5550dcc7a14c976741483c89"), 
     ObjectId("5550dcc7a14c976741483c8d") 
    ], 
    "collectionId" : ObjectId("5550dab4a14c9766418ff2af"), 
    "colours" : { 
     "WH" : [ 
      { 
       "on_sale" : false, 
       "size_code_id" : "S", 
       "sku" : "EP01L-WH1" 
      }, 

     ], 
     "BL" : [ 
      { 
       "on_sale" : false, 
       "size_code_id" : "S", 
       "sku" : "EP01L-BL1" 
      }, 

     ] 
    }, 
    "fabric" : { 
     "material" : [ 
      { 
       "_id" : ObjectId("5550dab4a14c9766418ff2c4"), 
       "value" : 100 
      } 
     ], 
     "type" : ObjectId("5550dab4a14c9766418ff2d1"), 
     "weight" : { 
      "ounce" : { 
       "min" : 4 
      }, 
      "gram" : { 
       "min" : 155 
      } 
     } 
    }, 

    "name" : { 
     "es-ES" : "CAMISETA MANGA LARGA HOMBRE", 
     "it-IT" : "T-SHIRT UOMO MANICA LUNGA", 
     "en-UK" : "MEN’S LONG SLEEVE T-SHIRT", 
     "fr-FR" : "T-SHIRT HOMME MANCHES LONGUES" 
    }, 
} 

> db.attributes.find({"name.en-UK": "Fabric"}).pretty(); 
{ 
    "_id" : ObjectId("5550dab4a14c9766418ff2dc"), 
    "values" : { 
     "source" : [ 
      { 
       "code" : 1, 
       "_id" : ObjectId("5550dab4a14c9766418ff2bf"), 
       "name" : { 
        "fr-FR" : "Coton", 
        "it-IT" : "Cotone", 
        "en-UK" : "Cotton", 
        "es-ES" : "Algodón" 
       } 
      }, 
      { 
       "code" : 2, 
       "_id" : ObjectId("5550dab4a14c9766418ff2c0"), 
       "name" : { 
        "fr-FR" : "Viscose", 
        "it-IT" : "Viscosa", 
        "en-UK" : "Viscose", 
        "es-ES" : "Viscosa" 
       } 
      }, 

     ], 
     "name" : [ 
      { 
       "_id" : ObjectId("5550dab4a14c9766418ff2cd"), 
       "name" : { 
        "en-UK" : "3-ply Loopback" 
       } 
      }, 

      { 
       "_id" : ObjectId("5550dab4a14c9766418ff2db"), 
       "name" : { 
        "en-UK" : "Woven Twill" 
       } 
      } 
     ] 
    }, 
    "name" : { 
     "en-UK" : "Fabric" 
    } 
} 

我想寫一個查詢,發現所有的風格文檔,其中面料.source是棉花或粘膠纖維,fabric.name是梭織斜紋布。

此查詢給了我所有具有fabric.type文件是棉或粘膠

> db.styles.find({ "fabric.type": { $in: [ ObjectId("5550dab4a14c9766418ff2cd"), ObjectId("5550dab4a14c9766418ff2db") ] } }).count(); 
3 
> 

但我怎麼找都在size_code_id是S的款式和顏色的WH

大加讚賞

回答

0

請檢查下面的查詢任何意見:

db.styles.find({ 
        $and:[ 
          { "colours.WH" :{ $exists:1 } } , 
          { "colours.WH.size_code_id" : "S"} 
         ] 
       } 
       ).pretty(); 

如果你需要找到 「WH」 和 「BL」,可以查詢象下面這樣:

db.styles.find({ 
        $or:[ 
          { 
           $and:[ 
             { "colours.WH" :{ $exists:1 } } , 
             { "colours.WH.size_code_id" : "S"} 
            ] 
          }, 
          { 
           $and:[ 
             { "colours.BL" :{ $exists:1 } } , 
             { "colours.BL.size_code_id" : "S"} 
            ] 
          } 
         ] 
       } 
       ).pretty(); 

PS:如果您需要查詢多種顏色,例如對於5-10種不同的顏色,那麼您應該爲所有這些公司動態構建查詢使用JavaScript語言構造的lors。

下面是動態地構造查詢被測試代碼:

// Colours you want to search 
var varray = new Array("WH","BL"); 
var fname = "colours", lname = "size_code_id"; 
var arr = new Array(); 
var mquery = {"$or":[]}; 

for(var i = 0; i < varray.length; i++) 
{ 
    var query = { "$and" : []}; 

    // This line will dynamically construct a key as "colours.WH" 
    var s1 = fname.concat(".").concat(varray[i]); 
    // This line will dynamically construct a key as "colours.WH.size_code_id" 
    var s2 = fname.concat(".").concat(varray[i]).concat(".").concat(lname); 
    var sub1 = {}, sub2 = {}; 

    sub1[s1] = { $exists:1 }; 
    sub2[s2] = "S"; 

    query["$and"].push(sub1); 
    query["$and"].push(sub2); 

    arr.push(query); 
} 
mquery["$or"] =arr; 

db.styles.find(mquery); 
+0

感謝,但我怎麼有多種顏色,例如,所有WH和BL? – khinester

+0

我編輯了我的帖子,請檢查它。 –

+0

很好,謝謝 – khinester