2

I posted a question yesterday - that, because if not knowing "how" to ask, it was poorly presented - So if you recognize the structure/data, please re-read, I hope I am able to present it in a clearer manner.

I want to return an array of items from multiple nested arrays of objects.

My ultimate goal is something that look like this...

{person:1, cost: ['23,543','56,556','8,500']},
{person:2, cost: []}

This lets me know 2 pieces of information, 1- it allows me to total across two or more arrays of objects, and 2- it lets me see persons who have NOT filled in information.

The Scenario:

I have a couple array of objects, and I want to find out if people are filling in 'cost' on an object.

So a model that looks like this.

person : {
    my_cars: [
        {
            make:
            model:
            cost:
        }
    ],
    my_motorcycles: [
        {
            make:
            model:
            cost:
        }
    ]
}

Some data will look like this (and this data corresponds to my above desired result...)

person : {
    _id: 1
    ,my_cars: [
        {
            make: 'ford'
            model: 'taurus'
            cost: null
        },
        {
            make: 'ford'
            model: 'mustang'
            cost:'23,543'
        }
        ,{
            make: 'lincoln'
            model: 'navigator'
            cost: '56,556'
        }
    ]
    ,my_motorcycles: [
        {
            make: 'ducati'
            model: ''
            cost: '8,500'
        }
        ,{
            make: 'yamaha'
            model: 'v-star 650'
            cost: ''
        }
    ]
}
,person : {
    _id: 2
    ,my_cars: [
        {
            make: 'ford'
            model: 'taurus'
            cost: null
        },
        ,{
            make: 'chevy'
            model: 'chevette'
            cost: null
        }
    ]
    ,my_motorcycles: [
        {
            make: 'harley-davidson'
            model: 'softtail'
            cost: ''
        }
        ,{
            make: 'honda'
            model: 'x650'
            cost: ''
        }
    ]
}

So what I am trying to do is unwind but if I try

$unwind{
    path: "$my_cars.cost"
}
,$unwind{
    path: "$my_motorcycles.cost"
}

this is not working...

Any help on getting to an output close to knowing the Number of an instance of a field in multiple nested arrays is greatly appreciated

1 Answer 1

2

The list of non-empty values can be retrieved after $filter run on both arrays ($concatArrays). You can use $type to check if value is a string:

db.collection.aggregate([
    {
        $project: {
            _id: 0,
            person: "$_id",                
            cost: {
                $filter: {
                    input: { $concatArrays: [ "$my_cars.cost", "$my_motorcycles.cost" ] },
                    cond: {
                        $and: [
                            { $eq: [ { $type: "$$this" }, "string" ] },
                            { $ne: [ "$$this", "" ] }
                        ]
                    }                           
                }
            }                        
        }
    }
])

Mongo Playground

Sign up to request clarification or add additional context in comments.

2 Comments

Perfect - thx Mickl - I had tried this - but it did not work. $project: { email:1, cost: { $filter: { input: { $concatArrays: [ '$my_cars.cost', '$vidPS', '$my_motorcycles.cost' ] }, as: "x", cond: { '$$x': { $nin: [ null, "" ] } } } - your solution worked... can you explain "$$this"
@jpmyob $$this simply refers to currently processed array element so it will be subsequent costs in this case

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.