2014-10-28 73 views
0

我是新来的节点和mongo,并有一个奇怪的问题,从mongo shell查看我的数据库的内容。我构建了一个简单的REST API,并且具有良好的CRUD功能,但即使数据似乎已保存,我仍无法从shell中找到它。从shell中查看mongo db内容

我的程序很简单;我从this example复制它。

有三个文件:package.json,server.js(主文件)和routes/wines.js。 Mongo db是像往常一样安装和配置的(监听端口27017)。我可以从shell中添加,更新,读取和删除收集项目,而不会出现任何问题。

的package.json:

{ 
    "name": "wine-cellar", 
    "description": "Wine Cellar Application", 
    "version": "0.0.1", 
    "private": true, 
    "dependencies": { 
    "express": "3.x", 
    "mongodb": "^1.4.19" 
    } 
} 

server.js

var express = require('express'), 
    wine = require('./routes/wines'); 

var app = express(); 

app.use(express.logger('dev'));  /* 'default', 'short', 'tiny', 'dev' */ 
app.use(express.bodyParser()); 

app.get('/wines', wine.findAll); 
app.get('/wines/:id', wine.findById); 
app.post('/wines', wine.addWine); 
app.put('/wines/:id', wine.updateWine); 
app.delete('/wines/:id', wine.deleteWine); 

app.listen(3000); 
console.log('Listening on port 3000...'); 

路线/ wines.js(很简单的REST API)

var mongo = require('mongodb'); 

var Server = mongo.Server, 
    Db = mongo.Db, 
    BSON = mongo.BSONPure; 

var server = new Server('localhost', 27017, {auto_reconnect: true}); 
db = new Db('winedb', server); 

db.open(function(err, db) { 
    if(!err) { 
     console.log("Connected to 'winedb' database"); 
     db.collection('wines', {strict:true}, function(err, collection) { 
      if (err) { 
       console.log("The 'wines' collection doesn't exist. Creating it with sample data..."); 
       populateDB(); 
      } 
     }); 
    } 
}); 

exports.findById = function(req, res) { 
    var id = req.params.id; 
    console.log('Retrieving wine: ' + id); 
    db.collection('wines', function(err, collection) { 
     collection.findOne({'_id':new BSON.ObjectID(id)}, function(err, item) { 
      res.send(item); 
     }); 
    }); 
}; 

exports.findAll = function(req, res) { 
    db.collection('wines', function(err, collection) { 
     collection.find().toArray(function(err, items) { 
      res.send(items); 
     }); 
    }); 
}; 

exports.addWine = function(req, res) { 
    var wine = req.body; 
    console.log('Adding wine: ' + JSON.stringify(wine)); 
    db.collection('wines', function(err, collection) { 
     collection.insert(wine, {safe:true}, function(err, result) { 
      if (err) { 
       res.send({'error':'An error has occurred'}); 
      } else { 
       console.log('Success: ' + JSON.stringify(result[0])); 
       res.send(result[0]); 
      } 
     }); 
    }); 
} 

exports.updateWine = function(req, res) { 
    var id = req.params.id; 
    var wine = req.body; 
    console.log('Updating wine: ' + id); 
    console.log(JSON.stringify(wine)); 
    db.collection('wines', function(err, collection) { 
     collection.update({'_id':new BSON.ObjectID(id)}, wine, {safe:true}, function(err, result) { 
      if (err) { 
       console.log('Error updating wine: ' + err); 
       res.send({'error':'An error has occurred'}); 
      } else { 
       console.log('' + result + ' document(s) updated'); 
       res.send(wine); 
      } 
     }); 
    }); 
} 

exports.deleteWine = function(req, res) { 
    var id = req.params.id; 
    console.log('Deleting wine: ' + id); 
    db.collection('wines', function(err, collection) { 
     collection.remove({'_id':new BSON.ObjectID(id)}, {safe:true}, function(err, result) { 
      if (err) { 
       res.send({'error':'An error has occurred - ' + err}); 
      } else { 
       console.log('' + result + ' document(s) deleted'); 
       res.send(req.body); 
      } 
     }); 
    }); 
} 

/*--------------------------------------------------------------------------------------------------------------------*/ 
// Populate database with sample data -- Only used once: the first time the application is started. 
// You'd typically not find this code in a real-life app, since the database would already exist. 
var populateDB = function() { 

    var wines = [ 
    { 
     name: "CHATEAU DE SAINT COSME", 
     year: "2009", 
     grapes: "Grenache/Syrah", 
     country: "France", 
     region: "Southern Rhone", 
     description: "The aromas of fruit and spice...", 
     picture: "saint_cosme.jpg" 
    }, 
    { 
     name: "LAN RIOJA CRIANZA", 
     year: "2006", 
     grapes: "Tempranillo", 
     country: "Spain", 
     region: "Rioja", 
     description: "A resurgence of interest in boutique vineyards...", 
     picture: "lan_rioja.jpg" 
    }]; 

    db.collection('wines', function(err, collection) { 
     collection.insert(wines, {safe:true}, function(err, result) {}); 
    }); 

}; 

当我测试从我的API终端使用curl命令,我没有问题。我可以创建,读取和编辑数据库中的数据,并且在终端上显示正常。

然而,mongo shell是一个不同的故事。当我运行“秀星展”,我看到我的winedb数据库中列出,但是当我运行查询就可以了,它的行为就好像数据库是空的:

> use winedb 
switched to db winedb 
> db.winedb.count() 
0 
> db.getCollection('winedb') 
winedb.winedb 
> db.count() 
2014-10-28T00:14:26.229-0400 TypeError: Property 'count' of object winedb is not a function 
> db.winedb.count() 
0 
> db.getCollection('winedb').find() 
> db.getCollection('winedb').count() 
0 

同时curl'ing一切都将继续正常工作。我怎样才能在mongo shell上看到我的数据?

回答

1

你的数据库称为winedb和集合称为wines,但你一直在试图找到文件的命名空间winedb.winedb,而不是winedb.wines。尝试在外壳下面:

> use winddb 
> db.wines.findOne() 
+0

感谢wdberkeley!你有没有可能为mongo推荐一个好的GUI? – 2014-10-28 17:25:08

1

您可以在here 上看到以下查询,并使用像robomongo这样的应用程序。这也会给你查询。

简单的查询得到的收集

> use winedb 
> show collections or tables //this will gives you list of collection 
> db.{collectionNAME}.find() 
> db.{collectionNAME}.count() 
> db.{collectionNAME}.find({id:''}) 
+0

还是什么都没有,可悲的是 – 2014-10-28 07:07:12

+0

{}集合名没有你把集合名称 – 2014-10-28 07:10:12

+0

你有没有看到任何收藏列表 – 2014-10-28 07:10:29