node.js MongoDB 여러 문서 삽입

· 6년 전 · 2499

여러 문서 삽입

 

MongoDB의 콜렉션에 여러 문서를 삽입하기 위해이 insertMany()메소드를 사용합니다 .

insertMany()메서드 의 첫 번째 매개 변수는 삽입하려는 데이터가 들어있는 객체의 배열입니다.

또한 오류 또는 삽입 결과로 작업 할 수있는 콜백 함수를 사용합니다.


"customers"컬렉션에 여러 문서 삽입 :

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  var dbo = db.db("mydb");
  var myobj = [
    { name: 'John', address: 'Highway 71'},
    { name: 'Peter', address: 'Lowstreet 4'},
    { name: 'Amy', address: 'Apple st 652'},
    { name: 'Hannah', address: 'Mountain 21'},
    { name: 'Michael', address: 'Valley 345'},
    { name: 'Sandy', address: 'Ocean blvd 2'},
    { name: 'Betty', address: 'Green Grass 1'},
    { name: 'Richard', address: 'Sky st 331'},
    { name: 'Susan', address: 'One way 98'},
    { name: 'Vicky', address: 'Yellow Garden 2'},
    { name: 'Ben', address: 'Park Lane 38'},
    { name: 'William', address: 'Central st 954'},
    { name: 'Chuck', address: 'Main Road 989'},
    { name: 'Viola', address: 'Sideway 1633'}
  ];
  dbo.collection("customers").insertMany(myobj, function(err, res) {
    if (err) throw err;
    console.log("Number of documents inserted: " + res.insertedCount);
    db.close();
  });
});

|
댓글을 작성하시려면 로그인이 필요합니다. 로그인

개발자팁

개발과 관련된 유용한 정보를 공유하세요. 질문은 QA에서 해주시기 바랍니다.

+
분류 제목 글쓴이 날짜 조회
PHP 6년 전 조회 3,247
MySQL 6년 전 조회 4,199
jQuery 6년 전 조회 3,677
node.js 6년 전 조회 2,728
node.js 6년 전 조회 2,467
node.js 6년 전 조회 2,696
node.js 6년 전 조회 2,681
node.js 6년 전 조회 2,438
node.js 6년 전 조회 2,583
node.js 6년 전 조회 2,332
node.js 6년 전 조회 2,313
node.js 6년 전 조회 2,782
node.js 6년 전 조회 1,913
node.js 6년 전 조회 2,041
node.js 6년 전 조회 2,500
node.js 6년 전 조회 2,288
node.js 6년 전 조회 2,366
node.js 6년 전 조회 2,181
node.js 6년 전 조회 2,494
node.js 6년 전 조회 2,293
node.js 6년 전 조회 2,727
node.js 7년 전 조회 2,137
node.js
[node.js]
7년 전 조회 2,064
node.js 7년 전 조회 8,737
node.js 7년 전 조회 3,802
node.js 7년 전 조회 2,440
node.js 7년 전 조회 2,543
node.js 7년 전 조회 2,126
node.js 7년 전 조회 3,427
node.js 7년 전 조회 2,265
🐛 버그신고