question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

mongoose - acsessing data of one collection from anther module

See original GitHub issue

Current behavior

i am using the mongoose module, lets say i have User collection which configured in the User module, Now i need this collection data in the Accounts module, i need to register it again in the Accounts module ? this is how my modules extract schemas:

Accounts Module

MongooseModule.forFeature([
     { name: 'Account', schema: AccountsSchema},
     { name: 'User', schema: UserSchema},
])

Users Module

MongooseModule.forFeature([
   { name: 'User', schema: UserSchema},
])

Is there a better way of doing it?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:15 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
silvelocommented, Feb 14, 2018

Remember to export, import the modules correctly

@Module({
    imports: [MongooseModule.forFeature([{ name: GAME_COLLECTION_NAME, schema: GameSchema }])],
    controllers: [GamesController],
    components: [GamesService],
    exports: [GamesService, MongooseModule],
})
export class GamesModule implements NestModule {}

@Module({
    imports: [MongooseModule.forFeature([{ name: USER_COLLECTION_NAME, schema: UserSchema }]), GamesModule, LinksModule],
    controllers: [UsersController],
    components: [UsersService],
    exports: [UsersService],
})
export class UsersModule implements NestModule {}

@Component()
export class UsersService {
    constructor(
        @InjectModel(GAME_COLLECTION_NAME) private readonly gameModel: Model<Game>,
        @InjectModel(USER_COLLECTION_NAME) private readonly userModel: Model<User>) { }
0reactions
lock[bot]commented, Sep 25, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

node.js - Access data from one collection to another MongoDB
You can populate the Job model and then retrieve user properties with: const job = await Job.findOne({}).populate('user').exec(); ...
Read more >
How to get data from 2 different collections of mongoDB using ...
After having a model, we can use method find() on the model of a particular collection to get documents of the collection. Syntax:...
Read more >
How can I insert a collection into another ... - MongoDB
I'm learning MongoDB/Mongoose by the docs and I'm trying to insert the second collection into the first collection, I know how to do...
Read more >
Express Tutorial Part 3: Using a Database (with Mongoose)
This article briefly introduces databases, and how to use them with Node/Express apps. It then goes on to show how we can use...
Read more >
How to Join Collections using Mongoose - ObjectRocket
This step-by-step tutorial will show you how to join collections using Mongoose and MongoDB.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found