Mongo find()产生{}以及预期结果

时间:2018-01-23 18:30:19

标签: javascript mongodb

您好我试图在我的查询中仅过滤掉video_trim,但代码仍会在过滤掉的内容中产生空对象

这是代码

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

    // Find some documents
    collection.find({}, {projection:{ _id: 0, name: 0, label: 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);
    });
  }
module.exports = {
findTrims: cb => {
    MongoClient.connect(url, (err, client) => {
      if (err) {
        return cb(err)
      }
      console.log('Connected successfully to server')

      const db = client.db(dbName)

      findTrims(db, (err, docs) => {
        if (err) {
          return cb(err)
        }

        // return your documents back to the given callback
        return cb(null, docs)
      })
    })
  }
}

查找的输出是这个

[{},{},{},{},{Video_trim:' ' }]

如何摆脱{}?

1 个答案:

答案 0 :(得分:1)

传递一些条件进行过滤。 F.E. collection.find({"Video_trim": { $exists: true }}, { _id: 0, name: 0, label: 0 })