티스토리 뷰
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로 웹 개발 시작하기