0

This is my code for getting all the documents/object in a collection. I've tried other ways. I am using nodejs and express. I am able to insert documents but i just cant retrieve them. Any point in the right direction is much appreciated.

 MongoClient.connect(url, function (err, db) {
        var cursor = db.collection('games').find({});
        console.log(cursor);
        db.close();
 });

This is the type of object I would like to see returned:

var game = {
        game_name: data.roomName,
        players: [{
            scktID: socket.id,
            name: "player1",
            score: 0,
            cards: [],
        }],
        card_count: 0,
        trump_suit: "na",
        current_player: 0,
        game_started: false
    };

Unfortunately I am receiving this back (a shortened version):

Cursor {
  pool: null,
  server: null,
  disconnectHandler:
   Store {
     s: { storedOps: [], storeOptions: [Object], topology: [Object] },
     length: [Getter] },
  bson: BSON {},
  ns: 'mydb.games',
  cmd:
   { find: 'mydb.games',
     limit: 0,
     skip: 0,
     query: {},
     readPreference: ReadPreference { preference: 'primary', tags: undefined, options: [Object] },
     slaveOk: true },
  options:
   { readPreference: ReadPreference { preference: 'primary', tags: undefined, options: [Object] },
     skip: 0,
     limit: 0,
     raw: undefined,
     hint: null,
     timeout: undefined,
     slaveOk: true,
     db:
      Db {
        domain: null,
        _events: {},
        _eventsCount: 0,
        _maxListeners: undefined,
        s: [Object],
        serverConfig: [Getter],
        bufferMaxEntries: [Getter],
        databaseName: [Getter] },
     promiseLibrary: [Function: Promise],
     disconnectHandler: Store { s: [Object], length: [Getter] } },
  topology:
   Server {
     domain: nul . . .

I am using nodejs, express and mongodb.

  • You forgot the `await`. `var cursor = await db.collection('games').findOne({});` Without that it actually is a "cursor", or indeed a `Promise`. You also likely mean `.findOne()` for a singular object, or you need `.toArray()` to convert from a "cursor" to what is actually usuable/serializable as an array. – Neil Lunn Aug 22 '17 at 12:08
  • Use this piece of code var result=db.collection('urls'); result.find().toArray(function(err,col){ console.log(col); }); here urls is my collection. – Syed Ayesha Bebe Aug 22 '17 at 13:13

0 Answers0