mongodb $ in,serach深深嵌套的子文档

时间:2015-09-28 20:49:57

标签: mongodb

我在mongo中有以下集合:

> 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"
    }
}

我正在尝试编写一个查询来查找所有样式文档,其中fabric.source是Cotton或Viscose,fabric.name是Woven Twill。

此查询为我提供了fabric.type为Cotton或Viscose

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

但如何找到size_code_id为S且颜色为WH的所有样式?

任何建议非常感谢

1 个答案:

答案 0 :(得分: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();

P.S :如果您需要查询多种颜色,请说明大约5到10种不同的颜色,那么您应该使用JavaScript语言结构动态构建所有这些颜色的查询。

以下是用于动态构建查询的TESTED代码:

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