mongodb/node-mongodb-native · error · MongoParseError
Option pkFactory must be an object with a createPk function,
Error message
Option pkFactory must be an object with a createPk function, got ${value} What it means
`pkFactory` overrides how `_id` values are generated and must be an object exposing a `createPk` function. The transform (src/connection_string.ts:952-961) requires `isRecord(value, ['createPk'])` AND `typeof value.createPk === 'function'`; otherwise it throws.
Source
Thrown at src/connection_string.ts:958
type: 'boolean'
},
name: {
target: 'driverInfo',
transform({ values: [value], options }) {
return { ...options.driverInfo, name: String(value) };
}
} as OptionDescriptor,
noDelay: {
default: true,
type: 'boolean'
},
pkFactory: {
default: DEFAULT_PK_FACTORY,
transform({ values: [value] }): PkFactory {
if (isRecord(value, ['createPk'] as const) && typeof value.createPk === 'function') {
return value as PkFactory;
}
throw new MongoParseError(
`Option pkFactory must be an object with a createPk function, got ${value}`
);
}
},
promoteBuffers: {
type: 'boolean'
},
promoteLongs: {
type: 'boolean'
},
promoteValues: {
type: 'boolean'
},
useBigInt64: {
type: 'boolean'
},
proxyHost: {
type: 'string'View on GitHub (pinned to 3366c21a63)
Solutions
- Provide `{ createPk: () => /* ObjectId or value */ }`
- Omit pkFactory to use the default ObjectId factory
- Ensure createPk is a function, not a method-name string
Example fix
// before
new MongoClient(uri, { pkFactory: () => new ObjectId() });
// after
new MongoClient(uri, { pkFactory: { createPk: () => new ObjectId() } }); Defensive patterns
Strategy: type-guard
Validate before calling
function isValidPkFactory(v) {
return v != null && typeof v === 'object' && typeof v.createPk === 'function';
}
if (options.pkFactory && !isValidPkFactory(options.pkFactory)) {
throw new TypeError('pkFactory must be { createPk: () => any }');
} Type guard
function isPkFactory(v) {
return !!v && typeof v === 'object' && typeof v.createPk === 'function';
} Prevention
- Wrap the generator function in an object: { createPk: fn }
- Omit pkFactory unless you truly need custom _id generation
- Do not pass a bare function as pkFactory
When it happens
Trigger: `{ pkFactory: {} }` (no createPk); `{ pkFactory: { createPk: 'x' } }` (not a function); `{ pkFactory: () => new ObjectId() }` (function instead of object); `{ pkFactory: null }`.
Common situations: Migrating from an older driver where a function was accepted; partial implementation of a custom factory; forgetting to wrap the function in an object.
Related errors
- Descriptors missing a type must define a transform
- ${name} must be an object with 'username' and 'password' pro
- authMechanism one of ${mechanisms}, got ${value}
- Invalid `serverApi` property; must specify a version from th
- Invalid server API version=${versionToValidate}; must be in
AI-assisted analysis of mongodb/node-mongodb-native@3366c21a63 (2026-08-04).
Data as JSON: /data/errors/49a9665946f7c63e.json.
Report an issue: GitHub.