Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
560 views
in Technique[技术] by (71.8m points)

node.js - Mongo Find() function won't exclude _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' } ]

Where did I go wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 }})

Read documentation here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...