Hey MongoDB community,
I’m encountering an odd issue. When I manually insert the document into MongoDB with a hardcoded userId
(as shown below), it works fine. But when I try to insert using args.userId
(coming from my application), the document gets created but without the userId
field.
Here’s the actual model data that gets created successfully when I hardcode the userId
:
{
"_id": { "$oid": "650c44c147cb927c814b0c86" },
"userId": { "$oid": "64ccc1b58bccb41a451a5a60" },
"sheetId": "BUL5UO7GB9",
"sheetName": "1695302848827_Yarn.xlsx",
"categorySlug": "YARN",
"isDeleted": false,
"deletedAt": null,
"createdAt": { "$date": { "$numberLong": "1695302849943" } },
"updatedAt": { "$date": { "$numberLong": "1695302849943" } },
"__v": { "$numberInt": "0" }
}
Issue Summary:
- Manual hardcoding:
When I directly assignuserId: "64ccc1b58bccb41a451a5a60"
, everything works and the document is created withuserId
. - Dynamic via
args.userId
:
When usinguserId: args.userId
from my backend payload, theuserId
field does not show up in the document, even though Iconsole.log(args.userId)
and it looks fine in the logs.
Things I have tried:
- Validated that
args.userId
is notundefined
ornull
. - Confirmed the
args.userId
is a valid 24-character hex string. - Converted
args.userId
to an ObjectId before inserting. - Logged payload before
create()
and it looks identical to the hardcoded one. - Checked for hidden whitespace or encoding issues.
Tech stack:
- MongoDB (using Mongoose ODM)
- Node.js backend
What could be causing this discrepancy?
Would really appreciate any help from the community!