Mongodb没有创建数据库

时间:2016-02-04 09:47:04

标签: node.js mongodb

我无法在启动服务器后在mongodb中创建我的数据库'sms-dev'但在我的控制台中它已成功打印连接到mongo。我将我的db模块保存在模块文件夹中作为db.js并将该模块导出到另一个文件中(config.js)在模型文件夹

这是db.js文件中的代码

UIImageView.deleteAllCaching()

我的config.js文件是

 var db = {
   // Connects to mongoDB
   connect: function(url, options) {
   mongoose.connect(url, options);
   mongoose.connection.on('open', function(){
  console.log("Connected to mongo successfully");
  });

 mongoose.connection.on('disconnect', function(){
  console.log("Mongo disconnected");
});

mongoose.connection.on('error',function (err) {
  console.log('Mongoose default connection error: ' + err);
});

process.on('SIGINT', function() {
  mongoose.connection.close(function () {
    console.log('Mongoose default connection disconnected through app termination');
    process.exit(0);
  });
});

}
}

module.exports = db;

我在server.js中将此数据库连接为

exports.database = {
  url: 'mongodb://127.0.0.1:27017/sms-dev',
  options: {
    db: { native_parser: true,safe:true },
    server: { poolSize: 10 }
   }
   }

7 个答案:

答案 0 :(得分:2)

这一行:

As soon as you create a record with that connection

从这个回答:Is it possible to create a new database in MongoDB with Mongoose?

对我来说。在我的情况下,我不得不手动:

  • 创建数据库
  • 创建一个集合
  • 在集合中创建记录

然后一切都很好。

答案 1 :(得分:0)

当您使用mongoose时,应该保持数据库连接非常简单。 mongoose.connect只应调用一次。这将为您的应用程序创建默认连接池。

// db.js

// Bring Mongoose into the app 
var mongoose = require( 'mongoose' ); 

// Create the database connection 
mongoose.connect('mongodb://127.0.0.1:27017/sms-dev'); 

// CONNECTION EVENTS
// When successfully connected
mongoose.connection.on('connected', function () {  
  console.log('Mongoose default connection open to ' + 'mongodb://127.0.0.1:27017/sms-dev');
}); 

// If the connection throws an error
mongoose.connection.on('error',function (err) {  
  console.log('Mongoose default connection error: ' + err);
}); 

// When the connection is disconnected
mongoose.connection.on('disconnected', function () {  
  console.log('Mongoose default connection disconnected'); 
});

// If the Node process ends, close the Mongoose connection 
process.on('SIGINT', function() {  
  mongoose.connection.close(function () { 
    console.log('Mongoose default connection disconnected through app termination'); 
    process.exit(0); 
  }); 
}); 

然后,您可以在文件中通过require mongoose轻松使用数据库连接。

// users.js

var mongoose = require( 'mongoose' ),  
    Users = mongoose.model('users'); 

答案 2 :(得分:0)

似乎我的mongodb没有正确锁定所以我删除了mongodb.lock文件并使用-repair选项运行

答案 3 :(得分:0)

一旦您将数据保存在数据库中 ,您可以通过运行命令

来查看数据库
show dbs 


const mongoose=require("mongoose")
mongoose.connect('mongodb://localhost:27017/username_db');
var db=mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
  console.log("connected successfully")
});
const Schema=mongoose.Schema;
const myschema=new Schema({
    name:String,
},{
    timestamps:true
});

var model=mongoose.model('myname',myschema);
 var data=new myname({
            name: 'xyz',
          })
          data.save((err)=>{
            res.send("Error in saving to database");
          })

答案 4 :(得分:0)

一旦您将数据保存在数据库中 ,您可以通过运行命令

来查看数据库

显示dbs

const mongoose=require("mongoose")
mongoose.connect('mongodb://localhost:27017/username_db');
var db=mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
  console.log("connected successfully")
});
const Schema=mongoose.Schema;
const myschema=new Schema({
    name:String,
},{
    timestamps:true
});

var model=mongoose.model('myname',myschema);
 var data=new myname({
            name: 'xyz',
          })
          data.save((err)=>{
            res.send("Error in saving to database");
          })

答案 5 :(得分:0)

这是一个相当老的帖子,我正在使用最新的软件包对其进行更新-此解决方案将解决以下env版本上的mongo db连接问题。

  1. 节点版本-11.5.0(节点-v)
  2. NPM。 -6.4.1(npm -v)
  3. 打字稿。 -3.8.3(tsc -v)
  4. Mongodb。 -5.9.15(package.json)

以下是为解决此问题而需要注意的步骤- 1.首先确认.ts文件中的所有更改都将反映相应.js文件中的更改。由于我的代码存在问题,因此未更新。 运行以下命令并验证.js文件

tsc --build tsconfig.json

如果未更新js文件,则只需删除delete.js文件并运行以上命令即可。这是非常简单的修复,但有时我们会忽略它。

  1. 因为它是打字稿代码。因此,需要复制下面的代码以进行验证。

    从“猫鼬”中导入*为m;

    导出类UserControl { RegisterUser(){ const uri =“ mongodb://127.0.0.1:27017 / User”; m.connect(uri, {useNewUrlPaerser:true, useUnifiedTopology:true, useFindAndModify:true, useCreateIndex:true});

    PrivateCertificate

    }

  2. 通过npm start运行您的解决方案。 验证是否创建了数据库集合?

如果没有。 首先在mongodb中创建名称为“ User”的数据库 使用mongo db罗盘。

比尝试。仍然看不到收藏集。

  1. 现在需要启动两个单独的控制台终端。

    • 转到文件夹和执行mongo c:\ program file \ MongoDb \ server \ 4.2 \ bin> mongo.exe

    • 在另一个终端类型mongod上, 它将启动您的mongo Damon。

现在尝试。由于上述步骤将稳定连接并显示1个连接处于活动状态。

希望此更新帮助。

答案 6 :(得分:-1)

这是重复:

Mongo db that does not exist but shows up in connection

如果您插入数据,您的数据库将被创建。