node.js MongoDB 여러 문서 삽입

· 6년 전 · 2497

여러 문서 삽입

 

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,244
MySQL 6년 전 조회 4,195
jQuery 6년 전 조회 3,675
node.js 6년 전 조회 2,726
node.js 6년 전 조회 2,464
node.js 6년 전 조회 2,693
node.js 6년 전 조회 2,678
node.js 6년 전 조회 2,433
node.js 6년 전 조회 2,580
node.js 6년 전 조회 2,326
node.js 6년 전 조회 2,310
node.js 6년 전 조회 2,778
node.js 6년 전 조회 1,908
node.js 6년 전 조회 2,038
node.js 6년 전 조회 2,498
node.js 6년 전 조회 2,286
node.js 6년 전 조회 2,364
node.js 6년 전 조회 2,174
node.js 6년 전 조회 2,490
node.js 6년 전 조회 2,287
node.js 6년 전 조회 2,723
node.js 7년 전 조회 2,134
node.js
[node.js]
7년 전 조회 2,059
node.js 7년 전 조회 8,730
node.js 7년 전 조회 3,799
node.js 7년 전 조회 2,437
node.js 7년 전 조회 2,540
node.js 7년 전 조회 2,125
node.js 7년 전 조회 3,424
node.js 7년 전 조회 2,262
🐛 버그신고