Add Data to MongoDB¶

傅皓君
2023-12-01

MongoDB Logo
ServerDriversCloudToolsGuides
Get MongoDB
Close ×
MongoDB Stitch

Introduction
Tutorials
Users & Authentication
MongoDB Atlas
    Overview
    Configure MongoDB
        Link a MongoDB Atlas Cluster
        Define Roles and Permissions
        Filter Incoming Queries
        Enforce a Document Schema
        Configure Advanced Rules
        Specify Cluster Read Preference
        Enable Wire Protocol Connections
    Work With MongoDB
        Add Data to MongoDB
        Find Documents in MongoDB
        Update Documents in MongoDB
        Delete Documents from MongoDB
        Watch for Document Changes
        Run Aggregation Pipelines
        Connect Over the Wire Protocol
    Reference
        MongoDB Actions
        Query Roles
        Query Filters
        Document Schemas
        Connection Strings
        Service Limitations
        CRUD & Aggregation APIs
GraphQL
MongoDB Mobile
Functions
Triggers
External Services
Values & Secrets
Application Deployment
Hosting
Troubleshooting
Stitch Administration
Application Logs
Client SDKs
Release Notes

Stitch > MongoDB Atlas > Work With MongoDB 

Add Data to MongoDB

On this page

Overview
    Data Model
    Snippet Setup
Methods
    Insert a Single Document
    Insert One or More Documents

Overview

The code snippets on this page demonstrate how to insert one or more documents into a MongoDB collection. Insert operations take the documents to add to MongoDB as an argument and return documents that describe the results of the operation.
Data Model

The examples on this page use a collection named store.items that models various items available for purchase in an online store. Each item has a name, an inventory quantity, and an array of customer reviews.

// store.items
{
_id: ,
name: ,
quantity: ,
reviews: [ { username: , comment: } ]
}

Snippet Setup

Functions JavaScript SDK Android SDK iOS SDK 

To use a code snippet in a function, you must first instantiate a MongoDB collection handle:

exports = function() {
const mongodb = context.services.get(“mongodb-atlas”);
const itemsCollection = mongodb.db(“store”).collection(“items”);
const purchasesCollection = mongodb.db(“store”).collection(“purchases”);
}

Methods
Insert a Single Document

You can insert a single document using the collection.insertOne() action.

The following snippet inserts a single item document into the items collection:

Functions JavaScript SDK Android SDK iOS SDK 

const newItem = {
“name”: “Plastic Bricks”,
“quantity”: 10,
“category”: “toys”,
“reviews”: [{ “username”: “legolover”, “comment”: “These are awesome!” }]
};

itemsCollection.insertOne(newItem)
.then(result => console.log(Successfully inserted item with _id: ${result.insertedId}))
.catch(err => console.error(Failed to insert item: ${err}))

Insert One or More Documents

You can insert multiple documents at the same time using the collection.insertMany() action.

The following snippet inserts multiple item documents into the items collection:

Functions JavaScript SDK Android SDK iOS SDK 

const doc1 = { “name”: “basketball”, “category”: “sports”, “quantity”: 20, “reviews”: [] };
const doc2 = { “name”: “football”, “category”: “sports”, “quantity”: 30, “reviews”: [] };

return itemsCollection.insertMany([doc1, doc2])
.then(result => {
console.log(Successfully inserted ${result.insertedIds.length} items!);
return result
})
.catch(err => console.error(Failed to insert documents: ${err}))

← Enable Wire Protocol Connections Find Documents in MongoDB →

© MongoDB, Inc 2008-present. MongoDB, Mongo, and the leaf logo are registered trademarks of MongoDB, Inc.
Was this page helpful?
Yes
No

 类似资料:

相关阅读

相关文章

相关问答