Pergunta

All i get from this code:

var views = db.books.find({"number":1}, {"views":1, _id:0}); console.log(views);

is this response:

{ _readableState:
  { highWaterMark: 16384,
    buffer: [],
    length: 0,
    pipes: null,
    pipesCount: 0,
    flowing: false,
    ended: false,
    endEmitted: false,
    reading: false,
    calledRead: false,
    sync: true,
    needReadable: false,
    emittedReadable: false,
    readableListening: false,
    objectMode: true,
    defaultEncoding: 'utf8',
    ranOut: false,
    awaitDrain: 0,
    readingMore: false,
    decoder: null,
    encoding: null },
 readable: true,
 domain: null,
 _events: {},
 _maxListeners: 10,
 _get: [Function] }
Foi útil?

Solução

You can use .findOne to return a single instance and get the result.

Find assumes you're querying for a collection of documents and return a cursor:

You can for example, convert it to an array:

db.books.find({"number":1}, {"views":1, _id:0}).toArray(function(err, results){
    console.log(results);
});

You can use .each to iterate through elements and .nextObject to get the next, they're all asynchronous obviously being IO operations in nodejs, here is the section on cursors in the manual.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top