我想从mongo db集合中控制日志数据,就是这样!这么简单,但它不会起作用......令人沮丧
以下是我的数据:
//this is what i have:
var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');
var request = require('request');
var mongoose = require('mongoose');
// setup server
var server = http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
// setup socket
var io = require('socket.io').listen(server);
http.createServer(function(request,response){
response.writeHead(200);
response.write(200);
response.end();
}).listen(8080);
// ^server setup, do your dirty down here:
var data;
// when request /
app.get('/', function(req, res) {
// render index
res.render('index.html');
// get database of markets
var markets = db.collection('ftse100');
console.log(markets);
markets.find(function(error,docs){
console.log(data);
console.log(docs);
data = docs;
console.log(data);
});
});
现在这就是我得到的。仍然没有数据通过?也许它与我从MongoDB中提取的集合或与MongoDB的某些东西有关?
答案 0 :(得分:2)
我正在使用Mongodb 3.6.3。不再有针对文档的find()函数;
docs.each(function(err, doc){
if(err) return console.err(err);
// Log document
console.log(doc)
});
或者您可以使用toArray()
db.collection( 'products' ).find().toArray(function(err, docs){
if (err) throw err;
console.log(docs);
});
答案 1 :(得分:1)
如果您只想在ftse100集合(英国数据库)中记录您的文档,这应该足够了:
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/uk');
// Wait until connection is established
mongoose.connection.on('open', function(err, doc){
console.log("connection established");
mongoose.connection.db.collection('ftse100', function(err, docs) {
// Check for error
if(err) return console.log(err);
// Walk through the cursor
docs.find().each(function(err, doc) {
// Check for error
if(err) return console.err(err);
// Log document
console.log(doc);
})
});
});