DEV Community

Donald Feury
Donald Feury

Posted on • Originally published at donaldfeury.xyz on

2 1

How to bulk insert documents into a MongoDB collection

Not only can you insert a single document into a collection with the insert method, it can also take in an array of documents for bulk insert operations.

The example below will insert all the documents in the given array into the collection called podcasts:

db.podcasts.insert([
    {
        "name": "Tech Over Tea",
        "episodeName": "#75 Welcome Our Hacker Neko Waifu | Cyan Nyan",
        "dateAired": ISODate("2021-08-02"),
        "odyseeURL": "https://odysee.com/@TechOverTea:3/tech-over-tea-ep75:5",
        "youtubeURL": "https://youtu.be/cc8ZTzi_fzg"
    },
    {
        "name": "Tech Over Tea",
        "episodeName": "Neckbeards Anonymous - Tech Over Tea #20 - feat Donald Feury",
        "dateAired": ISODate("2020-07-13"),
        "odyseeURL": "https://odysee.com/@TechOverTea:3/neckbeards-anonymous:5",
        "youtubeURL": "https://youtu.be/DzXLZpRS0xU"
    },
    {
        "name": "Tech Over Tea",
        "episodeName": "#34 The Return Of The Clones - feat Bryan Jenks",
        "dateAired": ISODate("2020-10-19"),
        "odyseeURL": "https://odysee.com/@TechOverTea:3/the-return-of-the-clones:a",
        "youtubeURL": "https://youtu.be/f4omYp7j05U"
    }
])

Enter fullscreen mode Exit fullscreen mode

There is also a method called insertMany that specifically only takes an array of documents as an argument, unlike insert which can operate on a single document as well as an array of documents.

More recent versions of MongoDB will give a deprecation warning when using insert and will want you to use insertMany when doing bulk inserts.

db.podcasts.insertMany([
    {
        "name": "Tech Over Tea",
        "episodeName": "#75 Welcome Our Hacker Neko Waifu | Cyan Nyan",
        "dateAired": ISODate("2021-08-02"),
        "odyseeURL": "https://odysee.com/@TechOverTea:3/tech-over-tea-ep75:5",
        "youtubeURL": "https://youtu.be/cc8ZTzi_fzg"
    },
    {
        "name": "Tech Over Tea",
        "episodeName": "Neckbeards Anonymous - Tech Over Tea #20 - feat Donald Feury",
        "dateAired": ISODate("2020-07-13"),
        "odyseeURL": "https://odysee.com/@TechOverTea:3/neckbeards-anonymous:5",
        "youtubeURL": "https://youtu.be/DzXLZpRS0xU"
    },
    {
        "name": "Tech Over Tea",
        "episodeName": "#34 The Return Of The Clones - feat Bryan Jenks",
        "dateAired": ISODate("2020-10-19"),
        "odyseeURL": "https://odysee.com/@TechOverTea:3/the-return-of-the-clones:a",
        "youtubeURL": "https://youtu.be/f4omYp7j05U"
    }
])

Enter fullscreen mode Exit fullscreen mode

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay