수

Node.js MongoDB Insert

· 2019-01-28 (월) 21:48:51 · 2866

Node.js MongoDB Insert

 

컬렉션에 삽입

 

insertOne()메서드 의 첫 번째 매개 변수는 삽입 할 문서의 각 필드 이름과 값을 포함하는 객체입니다.

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


예
"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: "Company Inc", address: "Highway 37" };
  dbo.collection("customers").insertOne(myobj, function(err, res) {
    if (err) throw err;
    console.log("1 document inserted");
    db.close();
  });
});

위의 코드를 "demo_mongodb_insert.js"라는 파일에 저장하고 파일을 실행하십시오.

"demo_mongodb_insert.js"를 실행하십시오.

C:\Users\Your Name>node demo_mongodb_insert.js

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

개발자팁

5,403건

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

+
분류 제목 글쓴이 날짜 조회
PHP 19-06-24 조회 3,186
PHP 19-06-09 조회 3,809
MySQL 19-04-01 조회 4,863
jQuery 19-03-20 조회 4,247
node.js 19-02-12 조회 3,351
node.js 19-02-12 조회 3,083
node.js 19-02-12 조회 3,346
node.js 19-02-07 조회 3,324
node.js 19-02-07 조회 3,063
node.js 19-02-07 조회 3,209
node.js 19-02-04 조회 2,944
node.js 19-02-04 조회 2,919
node.js 19-02-04 조회 3,367
node.js 19-01-31 조회 2,476
node.js 19-01-31 조회 2,625
node.js 19-01-31 조회 3,105
node.js 19-01-28 조회 2,867
node.js 19-01-28 조회 2,949
node.js 19-01-28 조회 2,791
node.js 19-01-22 조회 3,118
node.js 19-01-22 조회 2,883
node.js 19-01-22 조회 3,306
node.js 19-01-16 조회 2,753
node.js
[node.js]
19-01-16 조회 2,645
node.js 19-01-16 조회 9,323
node.js 19-01-12 조회 4,402
node.js 19-01-12 조회 2,999
node.js 19-01-12 조회 3,150
node.js 19-01-11 조회 2,696
node.js 19-01-11 조회 4,050