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 object in a sub array object not being set in Mongoose 6.0.13 #11011

Closed
richardlay opened this issue Nov 24, 2021 · 2 comments
Closed
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@richardlay
Copy link

Do you want to request a feature or report a bug?
Bug

What is the current behavior?

Illustrating the issue through an example, say we have the following schemas:

const TransactionSchema = new Schema({
    payments: [
        {
            id: { type: String },
            terminal: {
                _id: { type: Schema.Types.ObjectId, ref: "Terminal" },
                name: { type: String }
            } 
        }
    ]
});   

const TerminalSchema = new Schema({
    name: { type: String },
    apiKey: { type: String }
});

If you push an object to the payments array, where the object contains a terminal model object, the terminal field gets chopped. For example:

const Transaction = Mongoose.model("Transaction");
const Terminal = Mongoose.model("Terminal");
const transaction = new Transaction();
const terminal = new Terminal({
    name: "Front desk",
    apiKey: "somesecret"
});
transaction.payments.push({
    id: "testPayment",
    terminal: terminal
});

transaction.payments[0].terminal does not exist.
The transaction object looks like this:

{
    payments: [
        {
            id: "testPayment"
        }
    ]
}

If the current behavior is a bug, please provide the steps to reproduce.

What is the expected behavior?

Using Mongoose 5.13.3, under the same scenario above, transaction.payments[0].terminal exists.
The transaction object looks like this:

{
    payments: [
        {
            id: "testPayment",
            terminal: {
                _id: <some bson ID>,
                name: "Front desk"
            } 
        }
    ]
}

This behaviour was useful because we could store a snapshot of a model in a different collection and pick the fields we wanted using the Schema definition. In this example we don't want to snapshot the sensitive apiKey field.

Is there a way to keep this behaviour in Mongoose 6.0.13?

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

Node 16.3.0
Mongoose 6.0.13
MongoDB 4.2.17

@vkarpov15 vkarpov15 added this to the 6.0.16 milestone Nov 27, 2021
@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Nov 29, 2021
@IslandRhythms
Copy link
Collaborator

const mongoose = require('mongoose');
const {Schema} = mongoose;

const TransactionSchema = new Schema({
    payments: [
        {
            id: { type: String },
            terminal: {
                _id: { type: Schema.Types.ObjectId, ref: "Terminal" },
                name: { type: String }
            } 
        }
    ]
});   

const TerminalSchema = new Schema({
    name: { type: String },
    apiKey: { type: String }
});

const Transaction = mongoose.model("Transaction", TransactionSchema);
const Terminal = mongoose.model("Terminal", TerminalSchema);

async function run() {
    await mongoose.connect('mongodb://localhost:27017/', {useNewUrlParser: true,
    useUnifiedTopology: true,});
    await mongoose.connection.dropDatabase();
    const transaction = new Transaction();
    const terminal = new Terminal({
        name: "Front desk",
        apiKey: "somesecret"
    });
    console.log('transaction before push', transaction);
    console.log('terminal before push', terminal);
    transaction.payments.push({
        id: "testPayment",
        terminal: terminal
    });
    console.log('transaction after push', transaction);
}
run();

@vkarpov15 vkarpov15 modified the milestones: 6.0.16, 6.0.15 Dec 3, 2021
@vkarpov15
Copy link
Collaborator

As a workaround, you can do:

transaction.payments.push({
    id: "testPayment",
    terminal: terminal.toObject() // <-- convert to POJO first
});

vkarpov15 added a commit that referenced this issue Dec 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

3 participants