-
여러 뎁스의 reference 에 대하여 populate 하기데이터베이스 2024. 5. 16. 13:03728x90
https://mongoosejs.com/docs/populate.html
Mongoose v8.3.5: Query Population
Populate MongoDB has the join-like $lookup aggregation operator in versions >= 3.2. Mongoose has a more powerful alternative called populate(), which lets you reference documents in other collections. Population is the process of automatically replacing th
mongoosejs.com
Mongoose: How to populate across multiple levels and use field selection in the same time?
I have succesfully used population across multiple levels and field selection separately. But I couldn't find a way to do them both at the same time. Population across multiple levels Profile.find...
stackoverflow.com
const ProfileSchema = new Schema({ //We want to associate the user with the profile user: { //This will associate the user by his id type: Schema.Types.ObjectId, ref: "users", }, // We want a user friendly url handle: { type: String, // required: true, // Even though we are doing pre-validatioon max: 40, }, bio: { type: String, }, school: { type: String, }, major: { type: String, }, website: { type: String, }, location: { type: String, }, // Optional (might add it later) /** * status: { //where they are in their career type: String, required: true }, */ skills: { // In the form they will put skill1,skill2,.. and we will turn that into an array type: [String], // required: true, }, interests: { // In the form they will put interest1,interest2,.. and we will turn that into an array type: [String], // required: true, }, help_offers: { // In the form they will put help_offer1,help_offer2,.. and we will turn that into an array type: [String], // required: true, }, // Might be volunteering experience experience: [ { title: { type: String, required: true }, company: { type: String, required: true, }, location: { type: String, }, from: { type: Date, required: true, }, to: { type: Date, // Not required, going to be a checkbox }, current: { type: Boolean, default: false, }, description: { type: String, }, }, ], education: [ { school: { type: String, required: true }, degree: { type: String, required: true, }, fieldofstudy: { type: String, required: true, }, from: { type: Date, required: true, }, to: { type: Date, // Not required, going to be a checkbox }, current: { type: Boolean, default: false, }, description: { type: String, }, }, ], social: { youtube: { type: String, }, twitter: { type: String, }, facebook: { type: String, }, linkedin: { type: String, }, instagram: { type: String, }, }, date: { type: Date, dafault: Date.now, }, videoURL: { type: String, }, });
const UserSchema = new Schema({ name: { type: String, required: true, }, surname: { type: String, required: true, }, email: { type: String, required: true, }, password: { type: String, required: true, }, avatar: { type: String, }, birthday: { type: String, required: true, }, country: { type: String, required: true, }, date: { type: Date, default: Date.now, }, following: [ { type: Schema.Types.ObjectId, //The User has only ObjectId in uses array. ref helps us get full fields of User when we call populate() method. //https://bezkoder.com/mongodb-many-to-many-mongoose/ ref: "users", }, ], followers: [ { type: Schema.Types.ObjectId, ref: "users", }, ], });
Profile.findOne({ user: req.user.id }) .populate({ path: ["user", ["name", "avatar"]], populate: { path: "following" },
user 의 name, avatar 필드를 populate 하고 following 필드는 다시 populate 한다.
https://stackoverflow.com/questions/62607128/mongoose-populate-multiple-levels
Mongoose populate multiple levels
I have an users collection which has an orders key: orders:[{ type:mongoose.Schema.Types.ObjectId, ref:'Order' }] The orders schema has an array of products: contents:[{
stackoverflow.com
728x90'데이터베이스' 카테고리의 다른 글
업데이트 추가문제 해답 (0) 2024.04.24 몽고 DB 쿼리 - 연습과제 (0) 2024.04.23 MongoDB Aggregation 연습을 위한 users, todo 컬렉션 데이터 (0) 2023.08.09