0

Como puedo evitar que en la siguiente consulta, los campos de precio no sean arrays?

 db.products.aggregate(

// Pipeline
[
    // Stage 1
    {
        $match: {
        "Disabled": false,"AvailabilityPercentage": {"$gt": 0}
        }
    },

    // Stage 2
    {
        $unwind: {
            path : "$Colors",
            includeArrayIndex : "arrayIndex", // optional
            preserveNullAndEmptyArrays : false // optional
        }
    },

    // Stage 3
    {
        $unwind: {
            path : "$Colors.Sizes",
            includeArrayIndex : "arrayIndex", // optional
            preserveNullAndEmptyArrays : false // optional
        }
    },

    // Stage 4
    {
        $match: {
        "Colors.Sizes.Availability.IsAvailable": true
        }
    },

    // Stage 5
    {
        $unwind: {
            path : "$Colors.OrderedAssetsGroupList",
            includeArrayIndex : "arrayIndex", // optional
            preserveNullAndEmptyArrays : false // optional
        }
    },

    // Stage 6
    {
        $match: {
        "Colors.OrderedAssetsGroupList.Space": "default"
        }
    },

    // Stage 7
    {
        $unwind: {
            path : "$Colors.OrderedAssetsGroupList.OrderedAssets",
            includeArrayIndex : "arrayIndex", // optional
            preserveNullAndEmptyArrays : false // optional
        }
    },

    // Stage 8
    {
        $match: {
         "Colors.OrderedAssetsGroupList.OrderedAssets.Order":{"$gt": 0}
        }
    },

    // Stage 9
    {
        $unwind: {
            path : "$Colors.PriceFromList",
            includeArrayIndex : "arrayIndex", // optional
            preserveNullAndEmptyArrays : false // optional
        }
    },

    // Stage 10
    {
        $match: {
        "Colors.PriceFromList.Space": "default"
        }
    },

    // Stage 11
    {
        $match: {
        "Colors.PriceFromList.Prices.Sale":{"$type":2}
        }
    },

    // Stage 12
    {
        $group: {
        "_id": "$_id",
        "Title":{"$first": "$Title"},
        "SalePrice":   {"$first": "$Colors.PriceFromList.Prices.Sale" } ,
        "Price":{"$first": "$Colors.PriceFromList.Prices.Previous" },
        "MinPrice":   {"$first": "$Colors.PriceFromList.Prices.Sale" } ,
        "AssetId":   {"$first": "$Colors.OrderedAssetsGroupList.OrderedAssets._id" } ,
        "Slug":   {"$first": "$Slug"},
        "Reference" : {"$first": "$Colors._id"}}
    },
],

// Options
{
    cursor: {
        batchSize: 50
    }
}

// Created with Studio 3T, the IDE for MongoDB - https://studio3t.com/

);

y devuelve un documento de este typo:

{ 
"_id" : "104095", 
"Title" : "Titulo del producto", 
"SalePrice" : [
    "7.99"
], 
"Price" : [
    "7.99"
], 
"MinPrice" : [
    "7.99"
], 
"AssetId" : "865057", 
"Slug" : "Jersey mujer manga larga tacto terciopelo", 
"Reference" : NumberInt(9567)
}

1 respuesta 1

0

Solo hay que hacer un unwind de las propiedades que quieras que no sean una lista.

$unwind: {
        path : "$Colors.PriceFromList"
}

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.