Initializing mongo ref as string instead of ObjectId
See original GitHub issueBug Report
Accordingly to the docs
In case you want to specify relation to another model, later for populating, you can use @Prop() decorator as well. For example, if Cat has Owner which is stored in a different collection called owners, the property should have type and ref. For example:
import { Types } from 'mongoose';
import { Owner } from '../owners/schemas/owner.schema';
@Prop({ type: Types.ObjectId, ref: Owner.name })
owner: Owner;
So, I tried it…
Current behavior
Mongo is storing targetUser
as an string
{
"_id" : ObjectId("5fb2999b0b7d61a62cdc88b8"),
"targetUser" : "5faf0044ddfbfd93d02f6714",
"__v" : 0
}
Input Code
import { User } from "@/user/schemas/user.schema";
import { Prop, Schema } from "@nestjs/mongoose";
import { IsMongoId, IsOptional, IsString } from "class-validator";
import { Types } from "mongoose";
@Schema({ timestamps: true })
export default class NotificationDto {
@IsString()
@IsMongoId()
@IsOptional()
@Prop({ type: Types.ObjectId, ref: User.name })
targetUser?: User;
}
// controller
@Post()
async create(
@Body() notificationDto: NotificationDto,
): Promise<NotificationDto> {
const notification = await this.notyService.create(notificationDto);
return notification.toJSON();
}
// service
create(notification: NotificationDto): Promise<NotificationDocument> {
return this.notificationModel.create(notification);
}
// request payload
{
"targetUser": "5faf0044ddfbfd93d02f6714"
}
Expected behavior
Mongo should store targetUser
as an ObjectId
:
{
"_id" : ObjectId("5fb2999b0b7d61a62cdc88b8"),
"targetUser" : ObjectId("5faf0044ddfbfd93d02f6714"),
"__v" : 0
}
Environment
Nest version: 7.5.1
For Tooling issues:
- Node version: 12.19.0
- Platform:
$ cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04.1 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.1 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal
Others:
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (2 by maintainers)
Top Results From Across the Web
Initializing mongo ref as string instead of ObjectId #647
For example, if Cat has Owner which is stored in a different collection called owners, the property should have type and ref. For...
Read more >Mongoose Saved _id'ss as a string instead of ObjectId
Using Nodejs with Mongodb and Mongoose. I just found out that Mongoose/Mongodb has been saving the auto generated _id field as a string...
Read more >Swift - Convert ObjectID to String - Realm
how to convert ObjectID to String. My model _id is of type ObjectID, while the logic applied on it is of String.
Read more >MongoDB - _id as String instead of ObjectId ? Relationship ...
Ideally, it should be the unique field of the network entity. That means, store the nework entity's _id field (also) in the network...
Read more >Introduction to Spring Data MongoDB
A solid intro to using MongoDB in with Spring Data. ... to store an ObjectId in the database (either ObjectId, String or BigInteger...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@Pacheco95 try
mongoose.Schema.Types.ObjectId
instead ofmongoose.Types.ObjectId
, 2nd one works only with arrays - something like this@Prop({ type: [{ type: Types.ObjectId, ref: User.name }] })
I just tried using a field that point to a document from a different collection and also had issues. Also in my use case I sometimes have ObjectId and sometime populate. So shouldn’t the examples give the following?
When a user forgets to populate the types should indicate it’s an ObjectId