为什么我不能从.find()查询的结果中删除
我有一个看起来像这样的查询:
collection.find({}, {_id: 0}).toArray((err, result) => {
io.sockets.connected[clients[client.length-1]].emit('update chart state', result);
});
我的websocket正确地将数据发送到客户端,但_id
字段仍然存在于我收到的对象数组中。有人能指出我在这里缺少什么吗?
谢谢
回答如下:find
函数只接受一个参数 - 查询。返回的值是Cursor
,其中包含project
函数。因此,您可以使用以下内容:
collection.find({}).project({_id: 0}).toArray ...