【问题标题】:can you access a mongoose created database from mongo?您可以从 mongo 访问 mongoose 创建的数据库吗?
【发布时间】:2014-05-30 03:03:05
【问题描述】:

noob node.js,这里是 Express 和 MongoDB,在 Ubuntu 14 上运行;我通过以下方式在 express/nodejs 应用程序数据库中创建了一个猫鼬:

var mongoose = require('mongoose');
var express = require('express');
var app = express();
...
mongoose.connect("mongodb://localhost/firstExpress");

var UserSchema = new mongoose.Schema({
  name: String,
  email: String,
  age: Number 
});

var Users = mongoose.model('Users', UserSchema);

app.post('/users', function(req, res){
  var b = req.body;
  new Users({
    name: b.name,
    email: b.email,
    age: b.age
  }).save(function(err, userDoc){
    if (err) res.json(err);
    res.redirect('/users/' + userDoc.name);
  });
});

app.get('/users/:name', function(req, res){
  Users.find({ name: req.param.name }, function(err, docs){
    if (err){ 
      res.json(err);
    } else {
      res.json(doc[0]);
    } 
  });
});

发布请求有效,并将用户添加到用户群。有没有办法从 shell 中查看 mongo 中的这个 db?

即来自

sudo mongo --smallfiles

当我运行它并执行命令时

show dbs

我看不到 express 应用程序创建的数据库,也看不到它在任何数据库中包含的任何集合中。然而,当我重新启动节点应用程序并且不发出任何发布请求,但对用户调用 get 时,我得到了我之前创建的用户列表。有人可以建议吗?

【问题讨论】:

    标签: node.js mongodb express mongoose


    【解决方案1】:

    鉴于连接 url mongodb://localhost/firstExpress,我可以假设您的数据库名称是 firstExpress

    所以,只需使用mongo firstExpress 登录您的 mongo 服务器。要查看您的收藏,请使用show collections,您就会看到它们。

    【讨论】:

      【解决方案2】:

      使用 Robomongohttp://robomongo.org/ 以 Shell 为中心的跨平台 MongoDB 管理工具)查看您的数据库集合。

      添加到 localhost:27017 的新连接,您将看到所有包含数据的集合

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-09-27
        • 2017-11-01
        • 1970-01-01
        • 2010-09-25
        • 1970-01-01
        • 1970-01-01
        • 2020-04-20
        • 1970-01-01
        相关资源
        最近更新 更多