How to prevent mongo from returning a list of an item by using $ first

0

How can I avoid that in the following query, the price fields are not 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/

);

and returns a document of this type:

{ 
"_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)
}
    
asked by Adrian Godoy 01.02.2018 в 18:22
source

1 answer

0

You just have to make an unwind of the properties you want that are not a list.

$unwind: {
        path : "$Colors.PriceFromList"
}
    
answered by 05.02.2018 в 09:54