首页 > 解决方案 > Mongoose:如何跨多个级别填充并同时使用字段选择?

问题描述

我已经成功地分别使用了多个级别的人口和字段选择。但我找不到同时做这两件事的方法。
跨多个级别的人口

 Profile.findOne({ user: req.user.id })
  .populate({
        path: "user",
        populate: { path: "following" },
      })

字段选择:

 Profile.findOne({ user: req.user.id })
       .populate("user", ["name", "avatar"])

我想做这样的事情:
注意:这不起作用

 Profile.findOne({ user: req.user.id })
      .populate({
            path: ["user", ["name", "avatar"]],
            populate: { path: "following" },

这是Profile模型:

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,
  },
});

这是User模型:

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",
    },
  ],
});

标签: node.jsmongodbexpressmongooseorm

解决方案


I am not sure if I understood your aim by this

Profile.findOne({ user: req.user.id })
    .populate({
        path: ["user", ["name", "avatar"]],
        populate: { path: "following" },

但如果我理解的是你想要的,那么我们很幸运。这就是我得到的。

await RequestAproves.find()
         .populate('user', 'firstName surname email roleId')
         .populate({
              path: 'request',
              populate: [
                     { path: 'budgetItem', select: 'code name' },
                     { path: 'user', select: 'firstName surname' },
                   ],
             }
       )

More details will come later, Sorry, I couldnt use your schema description, Incase it doesnt work out, let me know, ill try to use your schema.


推荐阅读