0

I am using a mongoDB server that is located on another machine. My question is how can I display all the documents that were found with console.log? Currently my main.js script is this:

// 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.

3
  • Use print instead of console.log Commented Jul 6, 2016 at 14:28
  • @LuísSoares Now it says print is undefined Commented Jul 6, 2016 at 14:34
  • forget it. you're in NodeJS.. that answer if for MongoDB console :\ Commented Jul 6, 2016 at 14:43

2 Answers 2

2

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);
    }
});
Sign up to request clarification or add additional context in comments.

Comments

0

Well if you want to see each individuial doc then try function each() bellow code

 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 });
  });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.