「Node.js入門」createCollection() でMongoDBの コレクションを作成する

2020年11月23日

構文
変数名.createCollection(コレクションの名前, function (err, res) {some code}

サンプルコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://localhost:27017/testdb';
MongoClient.connect(url, { useNewUrlParser: true }, function (err, db) {
if (err) throw err;
console.log('database create');
var dbase = db.db("testdb");
dbase.createCollection('usertable', function (err, res) {
if (err) throw err;
console.log("collection create!");
db.close();
});
});
var MongoClient = require('mongodb').MongoClient; var url = 'mongodb://localhost:27017/testdb'; MongoClient.connect(url, { useNewUrlParser: true }, function (err, db) { if (err) throw err; console.log('database create'); var dbase = db.db("testdb"); dbase.createCollection('usertable', function (err, res) { if (err) throw err; console.log("collection create!"); db.close(); }); });
var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://localhost:27017/testdb';
MongoClient.connect(url, { useNewUrlParser: true }, function (err, db) {
    if (err) throw err;
    console.log('database create');
    var dbase = db.db("testdb");
    dbase.createCollection('usertable', function (err, res) {
        if (err) throw err;
        console.log("collection create!");
        db.close();
    });
});

 

Node.js

Posted by arkgame