猫鼬:如何填充多个级别并同时使用字段选择?

时间:2020-09-06 12:27:23

标签: node.js mongodb express mongoose orm

我分别成功使用了人口across multiple levelsfield selection。但是我找不到同时做它们的方法。
多层次人口

searchbar = driver.find_element_by_xpath('//input[@id="search"]')
searchButton = driver.find_element_by_xpath('//button[@id="search-icon-legacy"]')

字段选择:

 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

1 个答案:

答案 0 :(得分:1)

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.