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

Population fails when using "type" #7016

Closed
simllll opened this issue Sep 16, 2018 · 1 comment
Closed

Population fails when using "type" #7016

simllll opened this issue Sep 16, 2018 · 1 comment

Comments

@simllll
Copy link
Contributor

simllll commented Sep 16, 2018

I want to report a bug.

What is the current behavior?
When using a model that has a document defintion with "type", mongoose cannot populate the document correctly.

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

const mongoose = require('mongoose');

console.log(mongoose.version);

run().catch(error => console.error(error.stack));

async function run() {
    await mongoose.connect('mongodb://localhost:27017/test2');

    // Broken - population fails with "Null", and it tries to populate via "ModelA" (NOT B)
    const ModelA = mongoose.model('ModelA', new mongoose.Schema({
        bla: {
            select: false,
            type: {
                _reference: {
                    type: mongoose.Schema.Types.ObjectId,
                    ref: 'ModelB'
                },
            }
        }
    }));

    // here everything works - same model, only difference is that "defintion" is not done via "custom type".
    /* const ModelA = mongoose.model('ModelA', new mongoose.Schema({
        bla: {
            _reference: {
                type: mongoose.Schema.Types.ObjectId,
                ref: 'ModelB'
            }
        }
    })); */

    const ModelB = mongoose.model('ModelB', new mongoose.Schema({
        name: { type: String }
    }));

    const b = new ModelB({name: 'test'});

    await b.save();

    const a = new ModelA({bla: {_reference: b._id}});
    await a.save();

    const result = await ModelA.find({}, '+bla')
        .populate('bla._reference');

    console.log('result', JSON.stringify(result, false, 3));
}

This results into:

{
      "_id": "5b9eb13f0001254b2a2ea3f0",
      "bla": {
         "_reference": null <-- NULL
      },
      "__v": 0
   }

What is the expected behavior?
Mongoose should also be able to populate documents when they are defined via "type".

Please mention your node.js, mongoose and MongoDB version.
mongoose 5.2.15, v8.11.4, mongodb 3.6

@vkarpov15
Copy link
Collaborator

Mongoose doesn't support the syntax you're using. In the above example, Mongoose assumes that bla is mixed type. You need to define a separate schema for _reference, like type: new Schema({ _reference: ... })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants