Functions to resolve comomn problens with ObjectId
Functions to resolve comomn problens with ObjectId
npm install --save mongoose-id-utils
const { isObjectId, extractIdString, extractIdObjectId } = require('mongoose-id-utils')
const { isObjectId } = require('mongoose-id-utils')
const id = '5e4e2f3b8a4f4c001e0e4c2d'
console.log(isObjectId(id))
// Output: true
const { extractIdString } = require('mongoose-id-utils')
const idString = '5e4e2f3b8a4f4c001e0e4c2d'
const idInDocument = {
_id: '5e4e2f3b8a4f4c001e0e4c2d'
}
console.log(extractIdString(id))
// Output: 5e4e2f3b8a4f4c001e0e4c2d
console.log(extractIdString(idInDocument))
// Output: 5e4e2f3b8a4f4c001e0e4c2d
const { extractIdObjectId } = require('mongoose-id-utils')
const idString = '5e4e2f3b8a4f4c001e0e4c2d'
const idInDocument = {
_id: '5e4e2f3b8a4f4c001e0e4c2d'
}
console.log(extractIdObjectId(id))
// Output: ObjectId('5e4e2f3b8a4f4c001e0e4c2d')
console.log(extractIdObjectId(idInDocument))
// Output: ObjectId('5e4e2f3b8a4f4c001e0e4c2d')
Please consult CONTRIBUTING for guidelines on contributing to this project.
mongoose-id-utils © Marcos Porto, Released under the Apache-2.0 License.