0

My model called Residence

{
    "_id": { "$oid": "5d88dfe45feb4c06a5cfb762" },
    "spaces": [{ 
        "_id": { "$oid": "5d88dfe45feb4c06a5cfb76f" },
        "name": "Building 2",
        "subSpace": [
            {
                "_id": { "$oid": "5d88dfe45feb4c06a5cfb771" },
                "name": "Basement"
            }, 
            {
                "_id": { "$oid": "5d88dfe45feb4c06a5cfb770" }, 
                "name": "Floors" // Get only the name by Id
            }
        ]
    }
}

To find a spaceName by Id (OK)

exports.getSpaceNameById = (spaceId) => {
    return Residence.find({ 'spaces._id': spaceId }, { _id: 0, spaces: { $elemMatch: { _id: spaceId } } })
}

Now I want to have the subSpace name requested by Id.

But my dream would be to have both (query by subSpace Id, for this example : 5d88dfe45feb4c06a5cfb770) : spaceName / subSpaceName only with 1 request.

Thanks for your help.


UPDATE 1

I try this method but the response is an empty array

exports.getSubSpaceNameById = (spaceId) => {
    return Residence.aggregate([
        { $match: {'spaces.subSpace._id': spaceId}},
        { $project: {
            'spaces.subSpace': { $filter: {
                input: '$spaces.subSpace',
                as: 'mySubSpace',
                cond: { $eq: ['$$mySubSpace._id', spaceId]}
            }},
            _id: 0
        }}
    ])
}
  • No, as I say in my post update – Arnaud Lord Sesstyle Oct 30 '19 at 09:19
  • To clarify ( [as there is some discussion on this already](https://meta.stackoverflow.com/q/390887/2313887) ) the reason your question is closed is not actually a "suggestion" despite the comment currently automatically added. The question is closed because **I know** from many years of experience that this is indeed the answer. If you get an empty result, then you provided incorrect details. The method demonstrated is indeed correct. Please learn from it. – Neil Lunn Oct 30 '19 at 09:28

3 Answers3

0

Try something like this:

find({spaces._id:id})
Nikola Lukic
  • 4,001
  • 6
  • 44
  • 75
vicky
  • 415
  • 2
  • 10
0

The following should work for you. This query would return the whole document.

By name: db.collection.find({"spaces.subSpace.name": "Basement"})

By _id: db.collection.find({"spaces.subSpace._id": "YOUR_ID"})

Shihab
  • 2,641
  • 3
  • 21
  • 29
0

Try this query

   db.testers.aggregate([
    {
        $addFields:{
            "spaces":{
                $map:{
                    "input":"$spaces",
                    "as":"doc",
                    "in":{
                        $mergeObjects:[
                            "$$doc",
                            {
                                "subSpace":{
                                    $filter:{
                                        "input":"$$doc.subSpace",
                                        "as":"sn",
                                        "cond": {
                                        "$and": [
                                            { "$eq": [ "$$sn._id", "5d88dfe45feb4c06a5cfb770" ] },
                                        ]
                                    }

                                }
                            }
                            }
                        ]
                    }
                }
            }
        }
    }
]).pretty()
Mahesh Bhatnagar
  • 1,042
  • 1
  • 7
  • 8