Scott
Scott

Reputation: 442

Why can I not remove the _id key from the results of my .find() query?

I have a query which looks as such:

collection.find({}, {_id: 0}).toArray((err, result) => {
  io.sockets.connected[clients[client.length-1]].emit('update chart state', result);
});

My websocket is sending the data correctly to the client however the _id field is still present in the array of objects that I receive. Could someone point out to me what I am missing here?

Thank you

Upvotes: 1

Views: 27

Answers (1)

Kirk Larkin
Kirk Larkin

Reputation: 93093

The find function takes only one parameter - the query. The returned value is a Cursor, which contains a project function. You can, therefore, use something like this:

collection.find({}).project({_id: 0}).toArray ...

Upvotes: 2

Related Questions