简体   繁体   English

Node.js - 使用 console.log 显示 mongoDB 文档(无外壳)

[英]Node.js - Display mongoDB documents with console.log (No Shell)

I am using a mongoDB server that is located on another machine.我正在使用位于另一台机器上的 mongoDB 服务器。 My question is how can I display all the documents that were found with console.log?我的问题是如何显示在 console.log 中找到的所有文档? Currently my main.js script is this:目前我的 main.js 脚本是这样的:

// Connect to Mongo
MongoClient.connect('mongodb://10.254.17.115:27017/ExpressOrder', function(err, db) {

// Handle errors
assert.equal(null, err);
    // Insert data
    db.collection('ExpressOrder').insert({"SID":"24676637"});
    // Count data
    db.collection('ExpressOrder').find().count().then(function(numItems) {
        console.log(numItems); // Use this to debug
        callback(numItems);
    })
    // Display all data in db
    var found = db.collection('ExpressOrder').find();
    console.log(found); // Use this to debug
});

The data is properly inserted, and counts properly, but I just now need to know how do I display all the documents to the console with console.log.数据已正确插入,并正确计数,但我现在需要知道如何使用 console.log 将所有文档显示到控制台。

Have you tried this?你试过这个吗?

var found = db.collection('ExpressOrder').find();
found.each(function(err, doc) {
    assert.equal(err, null);
    if (doc != null) {
        console.log(doc);
    }
});

Well if you want to see each individuial doc then try function each() bellow code好吧,如果您想查看每个单独的文档,请尝试使用each()波纹管代码

 var db = mongoUtil.getDb();
  db.collection( 'products' ).find(function(err, docs){
    if (err) throw err;
    docs.each(function(err, doc){
      if(err) return console.err(err);
            // Log document
            console.log(doc)
    });
    res.render('shop/index', { title: 'Express', products:result });
  });

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

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