getting a single document instaid of many documents in the collection #164
-
The following is my code at this point:
import { EmailType } from '../../../src/account/entities/enums/emailTypes';
import { getObjectId } from 'mongo-seeding';
const scaffolding: any[] = [
// Admin
{
value: '[email protected]',
type: EmailType.ACCOUNT,
},
// Retail
{
value: '[email protected]',
type: EmailType.ACCOUNT,
},
// Angle
{
value: '[email protected]',
type: EmailType.ACCOUNT,
},
// Sophisticated
{
value: '[email protected]',
type: EmailType.ACCOUNT,
},
// Sophisticated institute
{
value: '[email protected]',
type: EmailType.ACCOUNT,
},
// issuer
{
value: '[email protected]',
type: EmailType.ACCOUNT,
},
// institute
{
value: '[email protected]',
type: EmailType.COMPANY,
},
// company
{
value: '[email protected]',
type: EmailType.COMPANY,
},
];
scaffolding.forEach((email, i) => {
email = Object.assign(email, {
_id: getObjectId(`email-${i}`),
});
});
export const emails = scaffolding;
import { register } from 'ts-node';
import path from 'path';
import { Seeder, SeederConfig } from 'mongo-seeding';
import dotenv from 'dotenv';
register();
dotenv.config();
const config = {
database: {
name: 'test',
protocol: 'mongodb+srv',
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
host: process.env.DB_HOST,
options: {
retryWrites: 'true',
w: 'majority',
},
},
dropDatabase: false,
};
const seeder = new Seeder(config);
seeder.log('ready...');
const collections = seeder.readCollectionsFromPath(path.resolve('./db/seeding-data'), {
extensions: ['js', 'json', 'ts'],
transformers: [Seeder.Transformers.replaceDocumentIdWithUnderscoreId],
});
console.log(collections);
seeder
.import(collections)
.then(() => {
console.log('Success');
})
.catch((err) => {
console.log('Error', err);
}); When i run this code it creates a collection with the expected name but then it populates it with a single document containing an array of documents. i was expecting to get each email to be a document on it's own. what am i doing wrong here? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Hi @gimyboya, export const emails = scaffolding; Please look into the docs https://github.com/pkosiec/mongo-seeding/blob/master/docs/import-data-definition.md#create-proper-directory-structure: Without named export it should work fine:
Let me know if it works for you. Cheers! |
Beta Was this translation helpful? Give feedback.
-
Thank you for the assistance. |
Beta Was this translation helpful? Give feedback.
Hi @gimyboya,
Looks like the problem is in the following line:
Please look into the docs https://github.com/pkosiec/mongo-seeding/blob/master/docs/import-data-definition.md#create-proper-directory-structure:
Without named export it should work fine:
Let me know if it works for you. Cheers!