0

我正在做一个 MERN 堆栈应用程序。我有一个 Profile 模型,其中包含一个名为 experience 的字段(它是一个数组)。当我调用 api 添加 1 个新体验对象时,虽然体验不是集合,但也会生成一个 ObjectId。我怎么知道 Mongoose(或 MongoDB)何时会自动生成该 ObjectId?

这是我的架构:

const ProfileSchema = Schema({
  user: {
    type: Schema.Types.ObjectId,
    ref: 'user'
  },
  handle: {
    type: String,
    required: true,
    max: 40
  },
  bio: {
    type: String
  },
  company: {
    type: String
  },
  website: {
    type: String
  },
  location: {
    type: String
  },
  status: {
    type: String,
    required: true
  },
  skills: {
    type: [String],
    required: true
  },
  githubusername: {
    type: String
  },
  experience: [
    {
      title: {
        type: String,
        required: true
      },
      company: {
        type: String,
        required: true
      },
      location: {
        type: String
      },
      from: {
        type: Date,
        required: true
      },
      to: {
        type: Date
      },
      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
      },
      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,
    default: Date.now
  }
});

api:

router.post(
  '/experience',
  passport.authenticate('jwt', { session: false }),
  (req, res) => {
    // Validate first
    let {errors, isValid} = Validator.validateExperience(req.body);
    if (!isValid) {
      return res.status(400).json(errors);
    }

    Profile.findOne({ user: req.user.id })
      .then(profile => {
        if (!profile) {
          errors.noprofile = 'There is no profile of this user.';
          return res.status(404).json(errors);
        }

        let newExp = {
          title: req.body.title,
          company: req.body.company,
          location: req.body.location,
          from: req.body.from,
          to: req.body.to,
          current: req.body.current,
          description: req.body.description,
        };

        // Add to experience array
        profile.experience.unshift(newExp);
        profile.save().then(profile => res.json(profile));
      })
  }
);

添加后:

"experience": [
{
  "current": true,
  "_id": "5b126c5fd16a74472136a6bb",
  "title": "Front-end Developer",
  "company": "Nash Tech",
  "from": "2017-09-18T00:00:00.000Z",
  "description": "Develop websites with current technology."
},
...
4

0 回答 0