> For the complete documentation index, see [llms.txt](https://degreetpro.gitbook.io/nestgram/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://degreetpro.gitbook.io/nestgram/config/modules/mongo-module.md).

# Mongo module

## Install @nestgram/mongo and mongoose

```nginx
npm i @nestgram/mongo mongoose
npm i @types/mongoose --save-dev
// or
yarn add @nestgram/mongo mongoose
yarn add @types/mongoose -D
```

## Connect mongoose

{% code title="app.module.ts" %}

```typescript
import { Module } from 'nestgram';
import { UseMongoConnection } from '@nestgram/mongo';

import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  controllers: [AppController],
  services: [AppService],
  modules: [
    UseMongoConnection('uri'), // pass uri here
  ],
})
export class AppModule {}
```

{% endcode %}

## Create schema

{% code title="user.schema.ts" %}

```typescript
import { Schema, Prop, NestSchema } from '@nestgram/mongo';
import { Document } from 'mongoose';

@Schema()
export class User {
  @Prop() email: string; // email field (string type)
  @Prop() name: string;
  @Prop() age: number; // age field (number type)
  @Prop({ type: [Object], default: [] }) articles: any[]; // params for field
}

export type UserDocument = User & Document; // export user document
NestSchema.reg(User); // reg schema
```

{% endcode %}

## Import schema

{% code title="app.module.ts" %}

```typescript
import { Module } from 'nestgram';
import { UseMongoConnection, UseMongo } from '@nestgram/mongo';

import { User } from './user.schema.ts';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  controllers: [AppController],
  services: [AppService],
  modules: [
    UseMongoConnection('uri'), // pass uri here
    UseMongo(User), // pass schemas here that will be passed to your service
  ],
})
export class AppModule {}
```

{% endcode %}

## Get the schema in the service

{% code title="app.service.ts" %}

```typescript
import { Service } from 'nestgram';
import { Model } from 'mongoose';
import { User } from './user.schema.ts';

@Service()
export class AppService {
  constructor(private readonly User: Model<User>) {}
}
```

{% endcode %}

## Then

You can then use the model in service methods and call it in controllers


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://degreetpro.gitbook.io/nestgram/config/modules/mongo-module.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
