
The ultimate solution for populating your MongoDB database :rocket:
Define MongoDB documents in JSON, JavaScript or even TypeScript files. Use JS library, install CLI or run Docker image to import them!
Mongo Seeding is a flexible set of tools for importing data into MongoDB database.
It's great for:
Define documents for MongoDB import in JSON, JavaScript or TypeScript file(s). To learn, how to do that, read the import data definition guide. To see some examples, navigate to the examples directory.
Use one of the Mongo Seeding tools, depending on your needs:
???
Profit!
There are many tools for MongoDB data import out there, including the official one - mongoimport. Why should you choose Mongo Seeding?
Every tool I found before creating Mongo Seeding support only JSON files. In my opinion, that is not the most convenient way of data definition. The biggest problems are data redundancy and lack of ability to write logic.
Imagine that you want to import 10 very similar documents into authors collection. Every document is identical - except the name:
{ "name": "{NAME_HERE}", "email": "example@example.com", "avatar": "https://placekitten.com/300/300" }
With every tool I've ever found, you would need to create 5 separate JSON files, or one file with array of objects. Of course, the latter option is better, but anyway you end up with a file looking like this:
[ { "name": "John", "email": "example@example.com", "avatar": "https://placekitten.com/300/300" }, { "name": "Joanne", "email": "example@example.com", "avatar": "https://placekitten.com/300/300" }, { "name": "Bob", "email": "example@example.com", "avatar": "https://placekitten.com/300/300" }, { "name": "Will", "email": "example@example.com", "avatar": "https://placekitten.com/300/300" }, { "name": "Chris", "email": "example@example.com", "avatar": "https://placekitten.com/300/300" } ]
It doesn't look good - you did probably hear about DRY principle.
Imagine that now you have to change authors' email. You would probably use search and replace. But what if you would need change the data shape completely? This time you can also use IDE features like multiple cursors etc., but hey - it's a waste of time. What if you had a much more complicated data shape?
If you could use JavaScript to define the authors documents, it would be much easier and faster to write something like this:
const names = ['John', 'Joanne', 'Bob', 'Will', 'Chris']; module.exports = names.map((name) => ({ name, email: 'example@example.com', avatar: 'https://placekitten.com/300/300', }));
Obviously, in JavaScript files you can also import other files - external libraries, helper methods etc. It's easy to write some data randomization rules - which are mostly essential for creating development sample data. Consider the following example of people collection import:
const { getObjectId } = require('../../helpers/index'); const names = ['John', 'Joanne', 'Bob', 'Will', 'Chris']; const min = 18; const max = 100; module.exports = names.map((name) => ({ firstName: name, age: Math.floor(Math.random() * (max - min + 1)) + min, _id: getObjectId(name), }));
The difference should be noticeable. This way of defining import data feels just right. And yes, you can do that in Mongo Seeding. But, JSON files are supported as well.
In multiple JSON files which contains MongoDB documents definition, it's easy to make a mistake, especially in complex data structure. Sometimes a typo results in invalid data. See the example below for people collection definition:
[ { "name": "John", "email": "john@mail.de", "age": 18 }, { "name": "Bob", "email": "bob@example.com", "age": "none" } ]
Because of a typo, Bob has email field empty. Also, there is a non-number value for age key.
The same problem would exist in JavaScript data definition. But, if you were able to use TypeScript, the situation slightly changes:
export interface Person { name: string; email: string; age: number; }
// import interface defined above import { Person } from '../../models/index'; const people: Person[] = [ { name: 'John', email: 'john@mail.de', age: 18, }, { name: 'Bob', emial: 'bob@example.com', // <-- error underlined in IDE age: 'none', // <-- error underlined in IDE }, ]; export = people;
If you used types, you would instantly see that you made mistakes - not only during import, but much earlier, in your IDE.
At this point some can say: “We had this for years — this is the purpose of mongoose!”. The problem is that importing a bigger amount of data with mongoose is painfully slow — because of the model validation. You can decide to use a faster approach, Model.collection.insert()
method, but in this case you disable model validation completely!
Also, starting from version 3.6, MongoDB supports JSON Schema validation. Even if you are OK with writing validation rules in JSON, you still have to try inserting a document into collection to see if the object is valid. It is too slow and cumbersome, isn’t it? How to solve this problem?
It’s simple. Use TypeScript. Compile time model validation will be much faster. And IDE plugins (or built-in support like in Visual Studio Code) will ensure that you won’t make any mistake during sample data file modification. Oh, and the last thing: If you have an existing TypeScript application which uses MongoDB, then you can just reuse all models for data import.
The Mongo Seeding CLI and Mongo Seeding Docker Image have TypeScript runtime built-in. It means that you can take advantage of static type checking in TypeScript data definition files (.ts extension).
Tools like this should be as flexible as possible. Some developers need just CLI tool, and some want to import data programmatically. Before writing Mongo Seeding, I needed a ready-to-use Docker image and found none. Dockerizing an application is easy, but it takes time.
That's why Mongo Seeding consists of:
All tools you'll ever need for seeding your MongoDB database.
Before you contribute to this project, read CONTRIBUTING.md file.


免费创建高清无水印Sora视频
Vora是一个免费创建高清无水印Sora视频的AI工具


最适合小白的AI自动化工作流平台
无需编码,轻松生成可复用、可变现的AI自动化工作流

大模型驱动的Excel数据处理工具
基于大模型交互的表格处理系统,允许用户通过对话方式完成数据整理和可视化分析。系统采用机器学习算法解析用户指令,自动执行排序、公式计算和数据透视等操作,支持多种文件格式导入导出。数据处理响应速度保持在0.8秒以内,支持超过100万行数据的即时分析。


AI辅助编程,代码自动修复
Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升 开发效率的理想工具。


AI论文写作指导平台
AIWritePaper论文写作是一站式AI论文写作辅助工具,简化了选题、文献检 索至论文撰写的整个过程。通过简单设定,平台可快速生成高质量论文大纲和全文,配合图表、参考文献等一应俱全,同时提供开题报告和答辩PPT等增值服务,保障数据安全,有效提升写作效率和论文质量。


AI一键生成PPT,就用博思AIPPT!
博思AIPPT,新一代的AI生成PPT平台,支持智能生成PPT、AI美化PPT、文本&链接生成PPT、导入Word/PDF/Markdown文档生成PPT等,内置海量精美PPT模板,涵盖商务、教育、科技等不同风格,同时针对每个页面提供多种版式,一键自适应切换,完美适配各种办公场景。


AI赋能电商视觉革命,一站式智能商拍平台
潮际好麦深耕服装行业,是国内AI试衣效果最好的软件。使用先进AIGC能力为电商卖家批量提供优质的、低成本的商拍图。合作品牌有Shein、Lazada、安踏、百丽等65个国内外头部品牌,以及国内10万+淘宝、天猫、京东等主流平台的品牌商家,为卖家节省将近85%的出图成本,提升约3倍出图效率,让品牌能够快速上架。


企业专属的AI法律顾问
iTerms是法大大集团旗下法律子品牌,基于最先进的大语言模型(LLM)、专业的法律知识库和强大的智能体架构,帮助企业扫清合规障碍,筑牢风控防线,成为您企业专属的AI法律顾问。


稳定高效的流量提升解决方案,助力品牌曝光
稳定高效的流量提升解决方案,助力品牌曝光


最新版Sora2模型免费使用,一键生成无水印视频
最新版Sora2模型免费使用,一键生成无水印视频
最新AI工具、AI资讯
独家AI资源、AI项目落地

微信扫一扫关注公众号