Đoạn mã trên trang này trình bày cách chèn một hoặc nhiều tài liệu vào bộ sưu tập MongoDB. Thao tác chèn lấy tài liệu để thêm vào MongoDB làm đối số và trả về tài liệu mô tả kết quả của thao tác
Mô hình dữ liệu¶
Các ví dụ trên trang này sử dụng một bộ sưu tập có tên
// Destructure Stitch JS SDK Components const { Stitch, RemoteMongoClient, BSON } = stitch;2 mô hình các mặt hàng khác nhau có sẵn để mua trong một cửa hàng trực tuyến. Mỗi mặt hàng có một
// Destructure Stitch JS SDK Components const { Stitch, RemoteMongoClient, BSON } = stitch;3, một hàng tồn kho
// Destructure Stitch JS SDK Components const { Stitch, RemoteMongoClient, BSON } = stitch;4 và một mảng khách hàng
// Destructure Stitch JS SDK Components const { Stitch, RemoteMongoClient, BSON } = stitch;5
sao chép
// store.items { _id: , name: , quantity: , reviews: [ { username: , comment: } ] }
Thiết lập đoạn mã¶
- Chức năng
- SDK JavaScript
- SDK Android
- SDK iOS
Để sử dụng một đoạn mã trong một hàm , trước tiên bạn phải khởi tạo một bộ điều khiển bộ sưu tập MongoDB.
sao chép
exports = function[] { const mongodb = context.services.get["mongodb-atlas"]; const itemsCollection = mongodb.db["store"].collection["items"]; const purchasesCollection = mongodb.db["store"].collection["purchases"]; }
Để sử dụng một đoạn mã trong một dự án JavaScript, trước tiên bạn phải làm như sau
1
Nhập phần phụ thuộc của Stitch¶
sao chép
Nhập từ CDN ¶
// Destructure Stitch JS SDK Components const { Stitch, RemoteMongoClient, BSON } = stitch;
- hoặc -
sao chép
Nhập từ Mô-đun ¶
// Import components of the Stitch JS SDK at the top of the file import { Stitch, RemoteMongoClient, BSON } from "mongodb-stitch-browser-sdk";
2
Khởi tạo Bộ điều khiển bộ sưu tập từ xa MongoDB¶
sao chép
________số 8
Để sử dụng một đoạn mã trong một dự án Android, trước tiên bạn phải làm như sau
1
Thiết lập dự án của bạn¶
Làm theo các bước trong hướng dẫn Thiết lập dự án di động MongoDB .
Ghi chú
Để biết thêm chi tiết về cách thiết lập ứng dụng Android của bạn để sử dụng Stitch, hãy tham khảo Tạo ứng dụng dành cho thiết bị di động có đồng bộ hóa hoặc Build a Local-Only Mobile App.
2
Nhập phần phụ thuộc của Stitch¶
Đối với các thao tác CRUD trên bộ sưu tập MongoDB từ xa, bạn sẽ sử dụng một hoặc nhiều câu lệnh
// Destructure Stitch JS SDK Components const { Stitch, RemoteMongoClient, BSON } = stitch;6 sau
sao chép
// Destructure Stitch JS SDK Components const { Stitch, RemoteMongoClient, BSON } = stitch;0
Để Đồng bộ hóa tài liệu giữa phiên bản MongoDB từ xa và phiên bản cục bộ, bạn cũng sẽ cần nhập các gói sau.
sao chép
// Destructure Stitch JS SDK Components const { Stitch, RemoteMongoClient, BSON } = stitch;1
Quan trọng
Nếu bạn sử dụng Đồng bộ hóa, hãy nhớ thêm
// Destructure Stitch JS SDK Components const { Stitch, RemoteMongoClient, BSON } = stitch;7 trước mỗi lệnh gọi cơ sở dữ liệu từ xa trong các ví dụ bên dưới, như trong ví dụ sau
sao chép
// Destructure Stitch JS SDK Components const { Stitch, RemoteMongoClient, BSON } = stitch;3
Để chỉ thực hiện các thao tác CRUD trên cơ sở dữ liệu cục bộ [trên thiết bị], hãy nhập các gói sau
sao chép
// Destructure Stitch JS SDK Components const { Stitch, RemoteMongoClient, BSON } = stitch;4
3
Khởi tạo Bộ điều khiển bộ sưu tập MongoDB¶
sao chép
Tệp hoạt động hàng đầu ¶
// Destructure Stitch JS SDK Components const { Stitch, RemoteMongoClient, BSON } = stitch;5
sao chép
Vào
// Destructure Stitch JS SDK Components
const { Stitch, RemoteMongoClient, BSON } = stitch;
8 ¶
exports = function[] { const mongodb = context.services.get["mongodb-atlas"]; const itemsCollection = mongodb.db["store"].collection["items"]; const purchasesCollection = mongodb.db["store"].collection["purchases"]; }0
Để sử dụng một đoạn mã trong một dự án iOS, trước tiên bạn phải làm như sau
1
Thiết lập dự án của bạn¶
Làm theo các bước trong hướng dẫn Thiết lập dự án di động MongoDB .
2
Nhập phần phụ thuộc của Stitch¶
sao chép
Trong phạm vi [e. g.
// Destructure Stitch JS SDK Components
const { Stitch, RemoteMongoClient, BSON } = stitch;
9] ¶
exports = function[] { const mongodb = context.services.get["mongodb-atlas"]; const itemsCollection = mongodb.db["store"].collection["items"]; const purchasesCollection = mongodb.db["store"].collection["purchases"]; }1
3
Khởi tạo MongoDB Stitch iOS SDK¶
sao chép
Khởi động ứng dụng [e. g. // Import components of the Stitch JS SDK at the top of the file
import {
Stitch,
RemoteMongoClient,
BSON
} from "mongodb-stitch-browser-sdk";
0] ¶
exports = function[] { const mongodb = context.services.get["mongodb-atlas"]; const itemsCollection = mongodb.db["store"].collection["items"]; const purchasesCollection = mongodb.db["store"].collection["purchases"]; }2
4
Khởi tạo Bộ điều khiển bộ sưu tập MongoDB¶
sao chép
Trong phạm vi [e. g.
// Destructure Stitch JS SDK Components
const { Stitch, RemoteMongoClient, BSON } = stitch;
9] ¶
exports = function[] { const mongodb = context.services.get["mongodb-atlas"]; const itemsCollection = mongodb.db["store"].collection["items"]; const purchasesCollection = mongodb.db["store"].collection["purchases"]; }3
Phương pháp¶
Chèn một tài liệu¶
Bạn có thể chèn một tài liệu bằng hành động
// Import components of the Stitch JS SDK at the top of the file import { Stitch, RemoteMongoClient, BSON } from "mongodb-stitch-browser-sdk";2
Đoạn mã sau chèn một tài liệu mục duy nhất vào bộ sưu tập
// Import components of the Stitch JS SDK at the top of the file import { Stitch, RemoteMongoClient, BSON } from "mongodb-stitch-browser-sdk";3
- Chức năng
- SDK JavaScript
- SDK Android
- SDK iOS
sao chép
exports = function[] { const mongodb = context.services.get["mongodb-atlas"]; const itemsCollection = mongodb.db["store"].collection["items"]; const purchasesCollection = mongodb.db["store"].collection["purchases"]; }4
sao chép
exports = function[] { const mongodb = context.services.get["mongodb-atlas"]; const itemsCollection = mongodb.db["store"].collection["items"]; const purchasesCollection = mongodb.db["store"].collection["purchases"]; }4
sao chép
exports = function[] { const mongodb = context.services.get["mongodb-atlas"]; const itemsCollection = mongodb.db["store"].collection["items"]; const purchasesCollection = mongodb.db["store"].collection["purchases"]; }6
sao chép
exports = function[] { const mongodb = context.services.get["mongodb-atlas"]; const itemsCollection = mongodb.db["store"].collection["items"]; const purchasesCollection = mongodb.db["store"].collection["purchases"]; }7
Chèn một hoặc nhiều tài liệu¶
Bạn có thể chèn nhiều tài liệu cùng lúc bằng cách sử dụng tác vụ
// Import components of the Stitch JS SDK at the top of the file import { Stitch, RemoteMongoClient, BSON } from "mongodb-stitch-browser-sdk";4
Đoạn mã sau chèn nhiều tài liệu mục vào bộ sưu tập
// Import components of the Stitch JS SDK at the top of the file import { Stitch, RemoteMongoClient, BSON } from "mongodb-stitch-browser-sdk";3
- Chức năng
- SDK JavaScript
- SDK Android
- SDK iOS
sao chép
exports = function[] { const mongodb = context.services.get["mongodb-atlas"]; const itemsCollection = mongodb.db["store"].collection["items"]; const purchasesCollection = mongodb.db["store"].collection["purchases"]; }8
sao chép
exports = function[] { const mongodb = context.services.get["mongodb-atlas"]; const itemsCollection = mongodb.db["store"].collection["items"]; const purchasesCollection = mongodb.db["store"].collection["purchases"]; }9
sao chép
// Destructure Stitch JS SDK Components const { Stitch, RemoteMongoClient, BSON } = stitch;0
sao chép
// Destructure Stitch JS SDK Components const { Stitch, RemoteMongoClient, BSON } = stitch;1
← Bật kết nối giao thức dây Tìm tài liệu trong MongoDB →
© MongoDB, Inc 2008-nay. MongoDB, Mongo và logo chiếc lá là các nhãn hiệu đã đăng ký của MongoDB, Inc