Node.js Mongoose.js string to ObjectId function
String to ObjectId:
const mongoose = require('mongoose');
const _id = mongoose.Types.ObjectId('4edd40c86762e0fb12000003');
Schema:
const Schema = new mongoose.Schema({ creater: mongoose.Schema.Types.ObjectId; }) //格式化创建者creater Schema.path('creater').set(setCreater); function setCreater(val) { return mongoose.Types.ObjectId(val); }