NodeJS find({}, {data:0}) still shows data field. Why?

I am trying to get a list of documents from my files collection without the data field. According to docs this should be easy with projection:

import { MongoClient } from "mongodb"

async function yeah() {
  const client = new MongoClient(connectionString)
  const conn = await client.connect();
  const myDB = url.parse(connectionString).pathname.substring(1)
  db = conn.db(myDB);
  console.log(await db.collection('files').find({}, { data: 0 }).next()) // or .toArray()
  console.log(await db.collection('files').find({}, { $unset: { data: true } }).next())

But the resulting document always includes the data field:

  _id: new ObjectId("5fb5304fb8d0c4001a263abc"),
  data: new Binary(Buffer.from("abc...", "hex"), 0),
  type: 'application/pdf',
  name: 'some.pdf',
  createdAt: 2020-11-18T14:31:43.166Z,
  updatedAt: 2020-11-18T14:31:43.166Z,
  __v: 0



node: v18.15.0
mongodb: ^5.1.0
mongodb server: mongo:4.4

Ok, this works:

find(filter, { projection: { data: 0 } })
1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.