node.js MongoDB 여러 문서 삽입

· 6년 전 · 2509

여러 문서 삽입

 

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,261
MySQL 6년 전 조회 4,206
jQuery 6년 전 조회 3,692
node.js 6년 전 조회 2,740
node.js 6년 전 조회 2,480
node.js 6년 전 조회 2,705
node.js 6년 전 조회 2,695
node.js 6년 전 조회 2,447
node.js 6년 전 조회 2,596
node.js 6년 전 조회 2,344
node.js 6년 전 조회 2,318
node.js 6년 전 조회 2,790
node.js 6년 전 조회 1,917
node.js 6년 전 조회 2,046
node.js 6년 전 조회 2,510
node.js 6년 전 조회 2,295
node.js 6년 전 조회 2,370
node.js 6년 전 조회 2,189
node.js 7년 전 조회 2,503
node.js 7년 전 조회 2,304
node.js 7년 전 조회 2,731
node.js 7년 전 조회 2,151
node.js
[node.js]
7년 전 조회 2,069
node.js 7년 전 조회 8,747
node.js 7년 전 조회 3,813
node.js 7년 전 조회 2,449
node.js 7년 전 조회 2,551
node.js 7년 전 조회 2,136
node.js 7년 전 조회 3,437
node.js 7년 전 조회 2,273
🐛 버그신고