简体   繁体   中英

MongoDB : Getting all the content of an object nested in an array using aggregation/projection

I'm having an issue returning an object that is inside of a nested array myShows.showChoices using projection/aggregation. (if that is even the correct way of approaching this).

Whenever I would try to use .find() by looking for the name or Id of the show, it would simply return the name or Id without any other content. I'm trying to return all the contents of the object. Including the name, overview, poster_path, ect... (Essentially the object itself)

My Schema:

const userSchema = new Schema({
email:{
    type: String,
    unique: true,
    lowercase: true,
    trim: true,
    required: 'Please Provide an email address'
},
name: {
    type: String,
    required: 'Please supply a name',
    trim: true,
},
myShows: {
    topRated: [],
    showChoices: []
});

An object I would $push into myShows.showChoices would look something like

{
 name: "Name of Show",
 poster_path:"/x2WKIbiwhLoWgLFbT2I0Gwq8U1J.jpg",
 genre_ids: [37, 878],
 overview: "show description",
 id: 63247
}

I have tried:

const show = await User.find(
    { _id: req.user.id },
    { showChoices: { $elemMatch: { name: "Name of Show" } }   }
)

UPDATE: 1

And several variations of projection/aggregation. This is my latest attempt. (Stuck on what to do after the last $match.)

const show = await User.aggregate([
            { "$match": {
              "_id" :  req.user._id,
              "myShows.showChoices.id": showIDVar
            }},
            { $unwind: "$myShows.showChoices" },
            { $match: {
                "myShows.showChoices.id": showIDVar
            }},
        ])

I think the issue with my past .find() queries were that I was searching only for the name or id of the show (not the _id)

const show = await User.find(
        { _id: req.user.id },
        { "myShows.showChoices": { name: "Name of Show" }   }
    )

Which would only return the name of the show itself. Is there anyway to return all the contents of the object by just querying the name?

Any advice on how to approach this?

        // Expected output is to have 
const show =
        {
         name: "Name of Show",
         poster_path:"/x2WKIbiwhLoWgLFbT2I0Gwq8U1J.jpg",
         genre_ids: [37, 878],
         overview: "show description",
         id: 63247
        };

You are almost there; just need to add $project stage:

const show = await User.aggregate([
            { "$match": {
              "_id" :  req.user._id,
              "myShows.showChoices.id": showIDVar
            }},
            { $unwind: "$myShows.showChoices" },
            { $match: {
                "myShows.showChoices.id": showIDVar
            }},
            { $project: {
                "myShows.showChoices.name": 1,
                "myShows.showChoices.poster_path": 1,
                "myShows.showChoices.genre_ids": 1,
                "myShows.showChoices.overview":1


            }},
        ])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM