Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mongoose 8 type: Buffer can set but will not be returned #15309

Closed
2 tasks done
rwmia opened this issue Mar 9, 2025 · 1 comment
Closed
2 tasks done

Mongoose 8 type: Buffer can set but will not be returned #15309

rwmia opened this issue Mar 9, 2025 · 1 comment
Labels
can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.

Comments

@rwmia
Copy link

rwmia commented Mar 9, 2025

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

8.x.x

Node.js version

22.x

MongoDB server version

5.x

Typescript version (if applicable)

No response

Description

Mongoose 8 seems to have an issue with type: Buffer field. It will set a buffer, but not return one. Will only work if the type is Any.

Mongoose will save type:Buffer as normal, but when retrieving the same document it will not return the type: Buffer. If you instead set field to type: Any, then it returns fine. Mongoose 6 worked as intended, however in Mongoose 7+ it seems to have stopped returning a buffer field.

In v8 when field is type: Buffer, saves as Binary.createFromBase64('xxx...), but will not return the field when retrieving the document.

When changing the field schema to type: Any, it "works" but saves as an object {"type":"buffer", "data": [...]}

Steps to Reproduce

Create a document with a schema field of type: Buffer.
Update the document field with a buffer.
Retrieve the document and the field will be undefined.

Instead of type: Buffer, use type: Any, or just {};
Update the document field with a buffer.
Retrieve the document and the field is returned as an object.

Expected Behavior

Should work as in Mongoose 6, saves type: Buffer and will retrieve it as well.

@vkarpov15
Copy link
Collaborator

I'm unable to repro, the following script shows that bufferField is a buffer both after create() and findOne(). Please modify the following script to demonstrate the issue you're seeing.

const mongoose = require('mongoose');

const MONGO_URI = 'mongodb://127.0.0.1:27017/mongoose_test';

const testSchema = new mongoose.Schema({
  bufferField: { type: Buffer },
});
const TestModel = mongoose.model('Test', testSchema);

run().catch(err => console.log(err));

async function run() {
  await mongoose.connect(MONGO_URI);
  await TestModel.deleteMany({});
  
  // Insert document with Buffer type
  const bufferData = Buffer.from('Hello, Mongoose 8!');
  const testDoc1 = await TestModel.create({ bufferField: bufferData });

  console.log(Buffer.isBuffer(testDoc1.bufferField));
  testDoc1.bufferField = Buffer.from('Hello!');
  await testDoc1.save();
  
  const { bufferField } = await TestModel.findById(testDoc1._id).orFail();
  console.log(Buffer.isBuffer(bufferField));

  process.exit(0);
}

@vkarpov15 vkarpov15 added the can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. label Mar 20, 2025
@rwmia rwmia closed this as completed Mar 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.
Projects
None yet
Development

No branches or pull requests

2 participants