티스토리 뷰

mongo db가 업데이트가 되면서 책에 있는 아래예제가 바로 동작하지 않는다.

https://gist.github.com/mithunsatheesh/8adad40b059866e892ed#file-chapter-02-js

 

chapter-02.js

GitHub Gist: instantly share code, notes, and snippets.

gist.github.com

 

아래 3가지 에러가 발생을 할 것이고, 

1. (node:18886) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.

2.

TypeError: db.collection is not a function

 

3.

(node:21860) DeprecationWarning: collection.insert is deprecated. Use insertOne, insertMany or bulkWrite instead.

 

 

userNewUrlParser, client.db, inertMany  변경으로 해결 하였다.

//require the mongoClient from mongodb module

var MongoClient = require('mongodb').MongoClient;

 

//mongodb configs

var connectionUrl = 'mongodb://localhost:27017/myproject',

sampleCollection = 'chapters';

 

//We need to insert these chapters into mongoDB

var chapters = [{

'Title': 'Snow Crash',

'Author': 'Neal Stephenson'

},{

'Title': 'Snow Crash',

'Author': 'Neal Stephenson'

}];

 

MongoClient.connect(connectionUrl, {useNewUrlParser: true}, function(err, client) {

 

console.log("Connected correctly to server");

var db = client.db('myproject');

 

// Get some collection

var collection = db.collection(sampleCollection);

 

collection.insertMany(chapters,function(error,result){

//here result will contain an array of records inserted

if(!error) {

console.log("Success :"+result.ops.length+" chapters inserted!");

} else {

console.log("Some error was encountered!");

}

client.close();

});

});

 

 

Ref : 노드JS와 몽고DB로 웹 개발 시작하기

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/08   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함