简体   繁体   English

在mongo db中看不到数据

[英]Cannot see the data in mongo db

This is how I insert data to a mongo db: 这是我将数据插入到mongo数据库的方法:

router.post('/', function (req, res, next) {
    var email = req.body.email;
    var user = new User({
        firsName: 'Juan',
        lastName: 'Dela Cruz',
        password: 'super-secret',
        email: email
    });
    user.save();
    res.redirect('/');
});

This is how I connect: 这是我的连接方式:

var mongoose = require('mongoose');

var appRoutes = require('./routes/app');

var app = express();
// mongoose.connect('localhost:27017/node-angular');
mongoose.connect('mongodb://localhost:27017/node-angular');

This is how I create my schema: 这就是我创建模式的方式:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var mongooseUniqueValidator = require('mongoose-unique-validator');

var schema = new Schema({
    firstName: {type: String, required: true},
    lastName: {type: String, required: true},
    password: {type: String, required: true},
    email: {type: String, required: true, unique: true},
    messages: [{type: Schema.Types.ObjectId, ref: 'Message'}]
});

schema.plugin(mongooseUniqueValidator);

module.exports = mongoose.model('User', schema);

I start the mongodb with this command in the command prompt with administrative privileges: 我使用具有管理特权的命令提示符在此命令中启动mongodb:

'C:\\WINDOWS\\system32>"C:\\Program Files\\MongoDB\\Server\\4.0\\bin\\mongo.exe"' 'C:\\ WINDOWS \\ system32>“ C:\\ Program Files \\ MongoDB \\ Server \\ 4.0 \\ bin \\ mongo.exe”'

then I try to access it using this command: 然后我尝试使用以下命令访问它:

use node-angular 使用节点角

switched to db node-angular 切换到db node-angular

db.users.find() db.users.find()

but I don't get the data in the database. 但我没有在数据库中获取数据。 I don't even know if I have successfully updated my database. 我什至不知道我是否已成功更新数据库。 can you please show me how to this right? 你能告诉我如何做到这一点吗? Thank you. 谢谢。

Everything is fine. 一切顺利。 You just need to change the collection name's first letter to upper-case. 您只需要将集合名称的首字母更改为大写即可。

Instead of, 代替,

db.users.find();

Use 采用

db.Users.find();

Hope it helps... 希望能帮助到你...

save function has a callback. 保存功能具有回调。 Try using that and check if there is some error that can be traced. 尝试使用它,并检查是否可以跟踪某些错误。 You can do something like this. 你可以做这样的事情。

user.save(function (err) { 
    if (err) return handleError(err); 
    // saved!
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM