Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
484 views
in Technique[技术] by (71.8m points)

mongodb - Mongoose populate within populated array and select a field

I have this dataset:

  {
    name: "Lakawon Island Resort",
    price_per_night: 2804.0,
    address: "Cadiz Viejo",
    amenities: 
      {
        tv: false,
        reservation: false,
    },
    reviews: [
        { 
            _id: 03020,
            user: 1030340,
            comment: 'Hi there'
        }
    ]
    image:
      "https://images.unsplash.com/photo-1591017403286-fd8493524e1e"
  },

And here is my Schema:

const reviewSchema = mongoose.Schema({
    rating: {
        type: Number,
        required: true
    },
    comment: {
        type: String,
        required: true
    },
    user: {
        type: mongoose.Schema.Types.ObjectId,
        required: true,
        ref: 'User'
    },
}, {
    timestamps: true
})

const resortSchema = mongoose.Schema({
    user: {
        type: mongoose.Schema.Types.ObjectId,
        required: true,
        ref: 'User'
    },
    name: {
        type: String,
        required: true
    },
    price_per_night: {
        type: Number,
        required: true,
    },
    address: {
        type: String,
        required: true
    },
    reviews: [reviewSchema], 
    image: {
        type: String
    },
    amenities: {
        tv: { type: Boolean, required: true, default: false },
        reservation: { type: Boolean, required: true, default: false },
        moderate_noise: { type: Boolean, required: true, default: false },
    },
}, { timestamps: true
})

const Resort = mongoose.model('Resort', resortSchema)

I am trying to pull up the list of array of object of users inside the reviews array which is also inside the resorts list. So far I got this which did not work:

const reviews = await Resort.findById(req.params.id).populate({ path: 'resort.reviews', populate: { path: 'reviews.user' } })

I want to seek for help on how to attain this. Any idea so I'll display something like this on my api?

{
    name: "Lakawon Island Resort",
    price_per_night: 2804.0,
    address: "Cadiz Viejo",
    amenities: 
      {
        tv: false,
        reservation: false,
    },
    reviews: [
        { 
            _id: 03020,
            user: {
                _id: 03030,
                name: 'Rick Martin'
              },
        },
       { 
            _id: 03021,
            user: {
                _id: 04031,
                name: 'Rick Martin'
              },
        }
            ]
            comment: 'Hi there'
        }
    ]
    image:
      "https://images.unsplash.com/photo-1591017403286-fd8493524e1e"
  },

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
await Resort.findById(req.params.id).populate({ path: 'reviews.user', select: 'name' })

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...