Làm cách nào để thêm dữ liệu vào tập bản đồ mongodb theo cách thủ công?
Đ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 Show 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 <script src="https://s3.amazonaws.com/stitch-sdks/js/bundles/4/stitch.js">script> <script> // Destructure Stitch JS SDK Components const { Stitch, RemoteMongoClient, BSON } = stitch; script>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 <script src="https://s3.amazonaws.com/stitch-sdks/js/bundles/4/stitch.js">script> <script> // Destructure Stitch JS SDK Components const { Stitch, RemoteMongoClient, BSON } = stitch; script>3, một hàng tồn kho <script src="https://s3.amazonaws.com/stitch-sdks/js/bundles/4/stitch.js">script> <script> // Destructure Stitch JS SDK Components const { Stitch, RemoteMongoClient, BSON } = stitch; script>4 và một mảng khách hàng <script src="https://s3.amazonaws.com/stitch-sdks/js/bundles/4/stitch.js">script> <script> // Destructure Stitch JS SDK Components const { Stitch, RemoteMongoClient, BSON } = stitch; script>5 sao chép // store.items { _id: <ObjectID>, name: <string>, quantity: <int>, reviews: [ { username: <string>, comment: <string> } ] } Thiết lập đoạn mã¶
Để 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 ¶ <script src="https://s3.amazonaws.com/stitch-sdks/js/bundles/4/stitch.js">script> <script> // Destructure Stitch JS SDK Components const { Stitch, RemoteMongoClient, BSON } = stitch; script> - 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 <script src="https://s3.amazonaws.com/stitch-sdks/js/bundles/4/stitch.js">script> <script> // Destructure Stitch JS SDK Components const { Stitch, RemoteMongoClient, BSON } = stitch; script>6 sau sao chép <script src="https://s3.amazonaws.com/stitch-sdks/js/bundles/4/stitch.js">script> <script> // Destructure Stitch JS SDK Components const { Stitch, RemoteMongoClient, BSON } = stitch; script>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 <script src="https://s3.amazonaws.com/stitch-sdks/js/bundles/4/stitch.js">script> <script> // Destructure Stitch JS SDK Components const { Stitch, RemoteMongoClient, BSON } = stitch; script>1 Quan trọng Nếu bạn sử dụng Đồng bộ hóa, hãy nhớ thêm <script src="https://s3.amazonaws.com/stitch-sdks/js/bundles/4/stitch.js">script> <script> // Destructure Stitch JS SDK Components const { Stitch, RemoteMongoClient, BSON } = stitch; script>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 <script src="https://s3.amazonaws.com/stitch-sdks/js/bundles/4/stitch.js">script> <script> // Destructure Stitch JS SDK Components const { Stitch, RemoteMongoClient, BSON } = stitch; script>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 <script src="https://s3.amazonaws.com/stitch-sdks/js/bundles/4/stitch.js">script> <script> // Destructure Stitch JS SDK Components const { Stitch, RemoteMongoClient, BSON } = stitch; script>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 ¶ <script src="https://s3.amazonaws.com/stitch-sdks/js/bundles/4/stitch.js">script> <script> // Destructure Stitch JS SDK Components const { Stitch, RemoteMongoClient, BSON } = stitch; script>5 sao chép Vào 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. 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. 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. 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
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
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 <script src="https://s3.amazonaws.com/stitch-sdks/js/bundles/4/stitch.js">script> <script> // Destructure Stitch JS SDK Components const { Stitch, RemoteMongoClient, BSON } = stitch; script>0 sao chép <script src="https://s3.amazonaws.com/stitch-sdks/js/bundles/4/stitch.js">script> <script> // Destructure Stitch JS SDK Components const { Stitch, RemoteMongoClient, BSON } = stitch; script>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 Làm cách nào để chèn dữ liệu vào MongoDB theo cách thủ công?Quy trình . Kết nối với phiên bản MongoDB của bạn Lấy cơ sở dữ liệu và bộ sưu tập. Chuyển sang cơ sở dữ liệu và bộ sưu tập mà bạn muốn làm việc. . Hiển thị kết quả của bạn. Nhiều thao tác ghi trong MongoDB trả về một đối tượng kết quả chứa thông tin về thao tác Kiểm tra kết quả của bạn Cách chèn thủ công dữ liệu vào la bàn MongoDB?Để chèn tài liệu vào bộ sưu tập của bạn. . Nhấp vào menu thả xuống Thêm dữ liệu và chọn Chèn tài liệu Chọn chế độ xem phù hợp dựa trên cách bạn muốn chèn tài liệu. Nhấp vào dấu ngoặc { } để xem JSON. Đây là chế độ xem mặc định. Nhấp vào biểu tượng danh sách cho chế độ Từng trường Tôi có thể lưu trữ bao nhiêu dữ liệu trong MongoDB Atlas?Cụm miễn phí M0 và cụm dùng chung M2/M5 có thể lưu trữ tài liệu với tối đa 50 cấp độ lồng nhau . Cụm M0 miễn phí và cụm chia sẻ M2/M5 không hỗ trợ xây dựng chỉ mục với bản dựng cuốn chiếu. |