Why turns the [Object] off?



  • var MongoClient = require('mongodb').MongoClient
        , format = require('util').format;
    
    MongoClient.connect('mongodb://127.0.0.1:27017/chat', function(err, db) {
        if (err) throw err;
    
        var collection = db.collection('test_insert');
        collection.remove({}, function(err, affected) {
            if (err) throw err;
    
            collection.insert({a: 2}, function(err, docs) {
    
                var cursor = collection.find({a: 2});
                cursor.toArray(function(err, results) {
                    console.dir(results);
                    // Let's close the db
                    db.close();
                });
            });
    
        });
    });
    

    There's a simple code for entering the database and then entering the console. That's what happens as a result of:

    [ { _id: ObjectID { _bsontype: 'ObjectID', id: [Object] }, a: 2 } ]
    

    How do we make the [Object] meanings?



  • Use the method

    ObjectId.toString()
    //В твоем случае
    results['_id'].toString()
    

    The docks are here: https://docs.mongodb.com/v3.2/reference/method/ObjectId/
    And then, https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Object/toString




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2