You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在MongoDB中创建自定义唯一ID时遇ValidationError:_id路径必填

Fixing Mongoose ValidationError: "_id: Path _id is required"

Let's break down why you're hitting this error and how to fix it.

Root Cause

When you explicitly define the _id field in your Mongoose schema (like you did with _id: {type: String, required: true}), Mongoose stops automatically generating an ObjectId for this field. By default, Mongoose creates an ObjectId for _id if the field isn't specified in the schema—but once you add it, you're telling Mongoose that you'll provide a value for it every time you create a new document. The error pops up because you're trying to save a Profile without passing a value for _id.

Solutions

1. Manually Provide the Custom _id When Saving

If you want to set the unique ID yourself each time, just include it when creating a new Profile instance:

const newProfile = new Profile({
  _id: "your-custom-unique-id-here", // e.g., "PROFILE-180001"
  seq: 180001,
  contact: "555-1234",
  altemail: "user@example.com",
  // Add other required fields...
});

newProfile.save()
  .then(profile => console.log("Saved profile:", profile))
  .catch(err => console.error("Save error:", err));

2. Auto-Generate Custom Unique IDs

If you want Mongoose to handle generating the custom ID automatically, use a pre-save hook to create the ID before the document is saved. Here are two common approaches:

Option A: Use UUIDs

First, install the uuid package (if you haven't already):

npm install uuid

Then add the pre-save hook to your schema:

const mongoose = require('mongoose');
const { v4: uuidv4 } = require('uuid');

const ProfileSchema = new mongoose.Schema({
  _id: { type: String, required: true },
  seq: { type: Number, default: 180000 },
  contact: String,
  altemail: String,
  presentaddress1: String,
  presentaddress2: String,
  zipecode: Number,
  updated_at: { type: Date, default: Date.now },
});

// Generate UUID for _id if not provided
ProfileSchema.pre('save', function(next) {
  if (!this._id) {
    this._id = uuidv4(); // Generates a unique string like "1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed"
  }
  next();
});

const Profile = mongoose.model('Profile', ProfileSchema);
Option B: Sequential IDs Using seq

If you want sequential IDs based on your seq field, you can generate them in the pre-save hook. Note: For production, you might want to use a separate counter collection to avoid race conditions, but here's a basic version:

ProfileSchema.pre('save', function(next) {
  if (!this._id) {
    this._id = `PROFILE-${this.seq}`; // e.g., "PROFILE-180000"
    this.seq += 1; // Increment seq for the next document
  }
  next();
});

3. Revert to Auto-Generated ObjectIds (If You Don't Need Custom IDs)

If you didn't intend to require a custom string ID, you can remove the _id field from your schema entirely. Mongoose will automatically generate an ObjectId for each new document:

const ProfileSchema = new mongoose.Schema({
  seq: { type: Number, default: 180000 },
  contact: String,
  altemail: String,
  presentaddress1: String,
  presentaddress2: String,
  zipecode: Number,
  updated_at: { type: Date, default: Date.now },
});

Summary

The core issue is that defining _id in your schema overrides Mongoose's auto-generation behavior. Choose the solution that fits your use case—either manually set the ID, auto-generate it with a hook, or let Mongoose handle it with ObjectIds.

内容的提问来源于stack exchange,提问作者chandra shekar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:37:27