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

 

posted @ 2018-08-27 16:36  yudis  阅读(134)  评论(0编辑  收藏  举报