Py学习  »  MongoDB

nodejs查询单个数据而不是来自mongodb集合的所有数据

Santosh • 5 年前 • 1650 次点击  

我正在尝试从MongoDB的集合中获取数据。我的代码只执行JSON格式的单行数据。但是当我控制台记录我的数据时,我可以看到所有的行数据。

const mongoose = require('mongoose');
const AllMinisters  = require('../models/allMinisters');
var db;
var mongodb = require("mongodb");

// Initialize connection once
mongoose.connect("******", { useNewUrlParser: true }, function(err, database) {
if(err) return console.error(err);
db = database;
// the Mongo driver recommends starting the server here because most apps *should* fail to start if they have no DB.  If yours is the exception, move the server startup elsewhere.
});

exports.getAllMinisters = (req,res,next)=>{
    db.collection("users").find({}, function(err, docs) {
        if(err) return next(err);
        docs.each(function(err, doc) {

        if(doc) {
            console.log(doc);
            var response = {
                statusCode: 200,
                headers:  { 'Content-Type': 'application/json' },
                body: doc
                }
                res.end(JSON.stringify(response));
        }
        });
    });
};

JSON中的输出为 enter image description here

但是,控制台报告显示 enter image description here

如何在JSON中显示所有行数据

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/38683
 
1650 次点击  
文章 [ 1 ]  |  最新文章 5 年前