最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

不能创建一个集合?

IT培训 admin 4浏览 0评论

不能创建一个集合?

我正在学习MongoDB的node.js。我已经安装了mongodb/downloads的完整包。

我创建了一个node.js文件,如下所示:

var MongoClient = require('mongodb').MongoClient;
var url = "mongo://localhost:27017/myDatabase";

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  console.log("Database created!");
  db.close();
});

在控制台中它显示我创建了一个数据库,但是当我打开mongo指南针并刷新时,我没有看到任何名为myDatabase的数据库。

所以接下来我尝试创建一个集合:

var MongoClient = require('mongodb').MongoClient;
var url = "mongo://localhost:27017/myDatabase";

MongoClient.connect(url, function(err, db) {
    if(err) throw err;
    db.createCollection("customers", function(err, res) {
        if(err) throw err;
        console.log("Collection created!");
        db.close();
    });
});

然后我得到这些错误:

Error: Invalid schema, expected `mongodb` or `mongodb+srv`
    at module.exports (D:\Node.js Projects\Node.js MongoDB\node_modules\mongodb\lib\url_parser.js:15:21)
    at connect (D:\Node.js Projects\Node.js MongoDB\node_modules\mongodb\lib\mongo_client.js:867:3)
    at connectOp (D:\Node.js Projects\Node.js MongoDB\node_modules\mongodb\lib\mongo_client.js:253:3)
    at executeOperation (D:\Node.js Projects\Node.js MongoDB\node_modules\mongodb\lib\utils.js:408:22)
    at MongoClient.connect (D:\Node.js Projects\Node.js MongoDB\node_modules\mongodb\lib\mongo_client.js:244:10)
    at Function.MongoClient.connect (D:\Node.js Projects\Node.js MongoDB\node_modules\mongodb\lib\mongo_client.js:472:22)
    at Object.<anonymous> (D:\Node.js Projects\Node.js MongoDB\CreateCollection\createcollection.js:4:13)
    at Module._compile (module.js:635:30)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)

任何人都可以帮助我为什么我没有在MongoDB Compass用户界面中看到myDatabase以及为什么我不能在前面的代码说创建数据库时创建集合?

回答如下:

连接网址应以“mongodb://”开头


您似乎正在使用与您所遵循的教程不匹配的较新版本的mongodb库

var MongoClient = require("mongodb").MongoClient;
var url = "mongodb://localhost:27017";
var dbName = "myDatabase";

MongoClient.connect(url, function(err, client) {
  if (err) throw err;

  var db = client.db(dbName);
  var collection = db.collection("customers");

  // you are using the collection but it won't be created until you insert a
  // document or create an index

  collection.find({}).toArray((err, docs) => {
    if (err) throw err;

    console.log(docs);

    client.close();
  });
});

最好去你的库版本匹配的official documentation

不能创建一个集合?

我正在学习MongoDB的node.js。我已经安装了mongodb/downloads的完整包。

我创建了一个node.js文件,如下所示:

var MongoClient = require('mongodb').MongoClient;
var url = "mongo://localhost:27017/myDatabase";

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  console.log("Database created!");
  db.close();
});

在控制台中它显示我创建了一个数据库,但是当我打开mongo指南针并刷新时,我没有看到任何名为myDatabase的数据库。

所以接下来我尝试创建一个集合:

var MongoClient = require('mongodb').MongoClient;
var url = "mongo://localhost:27017/myDatabase";

MongoClient.connect(url, function(err, db) {
    if(err) throw err;
    db.createCollection("customers", function(err, res) {
        if(err) throw err;
        console.log("Collection created!");
        db.close();
    });
});

然后我得到这些错误:

Error: Invalid schema, expected `mongodb` or `mongodb+srv`
    at module.exports (D:\Node.js Projects\Node.js MongoDB\node_modules\mongodb\lib\url_parser.js:15:21)
    at connect (D:\Node.js Projects\Node.js MongoDB\node_modules\mongodb\lib\mongo_client.js:867:3)
    at connectOp (D:\Node.js Projects\Node.js MongoDB\node_modules\mongodb\lib\mongo_client.js:253:3)
    at executeOperation (D:\Node.js Projects\Node.js MongoDB\node_modules\mongodb\lib\utils.js:408:22)
    at MongoClient.connect (D:\Node.js Projects\Node.js MongoDB\node_modules\mongodb\lib\mongo_client.js:244:10)
    at Function.MongoClient.connect (D:\Node.js Projects\Node.js MongoDB\node_modules\mongodb\lib\mongo_client.js:472:22)
    at Object.<anonymous> (D:\Node.js Projects\Node.js MongoDB\CreateCollection\createcollection.js:4:13)
    at Module._compile (module.js:635:30)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)

任何人都可以帮助我为什么我没有在MongoDB Compass用户界面中看到myDatabase以及为什么我不能在前面的代码说创建数据库时创建集合?

回答如下:

连接网址应以“mongodb://”开头


您似乎正在使用与您所遵循的教程不匹配的较新版本的mongodb库

var MongoClient = require("mongodb").MongoClient;
var url = "mongodb://localhost:27017";
var dbName = "myDatabase";

MongoClient.connect(url, function(err, client) {
  if (err) throw err;

  var db = client.db(dbName);
  var collection = db.collection("customers");

  // you are using the collection but it won't be created until you insert a
  // document or create an index

  collection.find({}).toArray((err, docs) => {
    if (err) throw err;

    console.log(docs);

    client.close();
  });
});

最好去你的库版本匹配的official documentation

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论