Hướng dẫn insert data mongodb node js - chèn dữ liệu nút mongodb js
Insert Into CollectionTo insert a record, or document as it is called in MongoDB, into a collection, we use the Show A document in MongoDB is the same as a record in MySQL The first parameter of the It also takes a callback function where you can work with any errors, or the result of the insertion: ExampleInsert a document in the "customers" collection: var MongoClient = require('mongodb').MongoClient; MongoClient.connect(url, function(err, db) { Run example » Save the code above in a file called "demo_mongodb_insert.js" and run the file: Run "demo_mongodb_insert.js" C:\Users\Your Name>node demo_mongodb_insert.js Which will give you this result: Note: If you try to insert documents in a collection that do not exist, MongoDB will create the collection automatically. Insert Multiple DocumentsTo insert multiple documents into a collection in MongoDB, we use the The first parameter of the It also takes a callback function where you can work with any errors, or the result of the insertion: ExampleInsert multiple documents in the "customers" collection: var MongoClient = require('mongodb').MongoClient; MongoClient.connect(url, function(err, db) { Run example » Save the code above in a file called "demo_mongodb_insert_multiple.js" and run the file: Run "demo_mongodb_insert_multiple.js" C:\Users\Your Name>node demo_mongodb_insert_multiple.js Which will give you this result: Number of documents inserted: 14 The Result ObjectWhen executing the The result object contains information about how the insertion affected the database. The object returned from the example above looked like this: { Các giá trị của các thuộc tính có thể được hiển thị như thế này: Thí dụTrả về số lượng tài liệu được chèn: Console.log (res.insertedcount) Sẽ tạo ra kết quả này: Trường _idNếu bạn không chỉ định trường Trong ví dụ trên không có trường Nếu bạn chỉ định trường Thí dụChèn ba bản ghi vào bảng "Sản phẩm", với các trường var Mongoclient = Yêu cầu ('MongoDB'). Mongoclient; var url = "MongoDB: // localhost: 27017/"; Mongoclient.connect (url, function (err, db) {& nbsp; if (err) ném err; & nbsp; var dbo = db.db ("mydb"); : 154, Tên: 'Sôcôla Heaven'}, & nbsp; & nbsp; & nbsp; {_id: 155, tên: 'Lemon ngon'}, & nbsp; ]; & nbsp; dbo.collection ("Sản phẩm"). & nbsp; & nbsp; & nbsp; db.close (); & nbsp;});}); Chạy ví dụ » Lưu mã ở trên trong một tệp có tên là "demo_mongodb_insert_id.js" và chạy tệp: Chạy "demo_mongodb_insert_id.js" C: \ Users \ Your Name> Node demo_mongodb_insert_id.js Điều này sẽ cung cấp cho bạn kết quả này: {& nbsp; Kết quả: {OK: 1, n: 3}, & nbsp; ops: [& nbsp; & nbsp; & nbsp; {_id: 154, Tên: 'Sôcôla Heaven}, & nbsp; & nbsp; & nbsp; {_id: 155, Tên: 'Lemon ngon}, & nbsp; & nbsp; & nbsp; {_id: 156, Tên: 'Vanilla Dream}], & nbsp; ChènCount: 3, & nbsp; ChènedIdIDS: [& nbsp; & nbsp; & nbsp; 154, & nbsp; & nbsp; & nbsp; 155, & nbsp; & nbsp; & nbsp; 156]} Node JS đẩy dữ liệu như thế nào trong MongoDB?Chèn vào bộ sưu tập.. var url = "MongoDB: // localhost: 27017/"; Mongoclient. kết nối (url, hàm (err, db) {. if (err) ném err; var dbo = db .. DB ("MyDB"); var myobj = {name: "công ty inc", địa chỉ: "Quốc lộ 37"}; DBO. Bộ sưu tập ("Khách hàng") .. INSTONE (myObj, function (err, res) {if (err) ném err; bảng điều khiển .. Làm thế nào thủ công dữ liệu trong MongoDB?Để chèn dữ liệu vào Bộ sưu tập MongoDB, bạn cần sử dụng phương thức Chèn () hoặc lưu () của MongoDB.use MongoDB's insert() or save() method.
Làm thế nào để bạn chèn vào MongoDB?Vỏ MongoDB cung cấp các phương pháp sau để chèn tài liệu vào một bộ sưu tập:.. Để chèn một tài liệu duy nhất, sử dụng DB.thu thập.chèn (). Để chèn nhiều tài liệu, sử dụng DB.thu thập.Chèn (). Làm thế nào để bạn tạo một bộ sưu tập và chèn dữ liệu trong nút MongoDB JS?Cách thêm bản ghi hoặc tài liệu trong bộ sưu tập MongoDB bằng Node JS và Mongoose.. Bước 1: Tạo dự án nút .. Bước 2: Cài đặt phụ thuộc NPM .. Bước 3: Kết nối với MongoDB .. Bước 4: Xây dựng lược đồ với Mongoose .. Bước 5: Xác định các tuyến đường với biểu hiện .. Bước 6: Tạo biểu mẫu để thêm bản ghi .. Bước 7: Xây dựng tệp máy chủ .. |