Mongo Find()函数不会排除_id [英] Mongo Find() function won't exclude _id

查看:86
本文介绍了Mongo Find()函数不会排除_id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我似乎无法使排除_id正常工作,这是代码

Hello I can't seem to get the exclude _id to work, here is the code

const findLabels = (db, cb) => {
  // Get the documents collection
  const collection = db.collection(documentName);

  // Find some documents
  collection.find({}, { _id: 0 }).toArray((err, docs) => {
    // An error occurred we need to return that to the given 
    // callback function
    if (err) {
      return cb(err);
    }

    assert.equal(err, null);
    console.log("Found the following records");
    console.log(docs)

    return cb(null, docs);
  });
}

这是控制台日志上的输出

Here is the output on the console log

Found the following records
[ { _id: 5a5ee78cc130e727a3b1fdb6, name: 'Downgradeklajds' },
  { _id: 5a5ee794c130e727a3b1fdb7, Pizel: '00:00:07' } ]

我哪里出错了?

推荐答案

我认为指定投影的正确方法是使用"fields"或"projection"属性,具体取决于驱动程序的版本.

I think the correct way to specify a projection is to use the "fields" or "projection" property, depends on the version of your driver.

collection.find({}, {projection:{ _id: 0 }})

此处阅读文档.

这篇关于Mongo Find()函数不会排除_id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆