[英]How to create database collection with typeorm on Atlas MongoDb?
I have been using Mysql and pg Typeorm with Nestjs on projects which work fine.我一直在将 Mysql 和 pg Typeorm 与 Nestjs 一起用于运行良好的项目。 I decided to use mongo Atlas, I have connected successfully to Atlas with no error but collection not created.
我决定使用 mongo Atlas,我已成功连接到 Atlas,没有错误,但未创建集合。 Whenever I use mysql with these steps, the table will be created.
每当我通过这些步骤使用 mysql 时,都会创建表。
That is not happening with mongo, maybe I am not doing something right mongo没有发生这种情况,也许我做的不对
I also installed dependency我还安装了依赖项
npm i --save @nestjs/typeorm typeorm @types/mongodb mongodb
ormconfig.json ormconfig.json
{
"type": "mongodb",
"url": "mongodb+srv://dbUser:<password>@<database>.mongodb.net/test?retryWrites=true&w=majority",
"useNewUrlParser": true,
"synchronize": true,
"useUnifiedTopology": true,
"logging": true,
"ssl": true,
"entities": [
"dist/**/*.entity{.ts,.js}"
]
}
app.module.ts app.module.ts
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AppController } from './app.controller';
import { AppService } from './app.service';
@Module({
imports: [TypeOrmModule.forRoot()],
controllers: [AppController],
providers: [AppService],
})
export class AppModule { }
test.entity.ts测试实体.ts
import { Entity, Column, CreateDateColumn, ObjectIdColumn, ObjectID } from 'typeorm';
@Entity('Test')
export class TestEntity {
@ObjectIdColumn()
id: ObjectID;
@Column()
firstName: string;
@Column()
lastName: string;
@Column()
age: number;
@CreateDateColumn()
created: Date;
}
I appreciate your effort我感谢你的努力
The collection will be created when you attempt to insert your first document.当您尝试插入第一个文档时,将创建该集合。
Just another tip.只是另一个提示。 I noticed in your entity you are inserting a '@CreateDateColumn()'.
我注意到您在您的实体中插入了一个“@CreateDateColumn()”。 MongoDB ObjectId contains a timestamp, which represents the creation date of the document.
MongoDB ObjectId 包含一个时间戳,它表示文档的创建日期。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.