Skip to content

Nested array references losing population after a new version is assigned #9293

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

Closed
pedroh2604 opened this issue Jul 29, 2020 · 0 comments
Closed
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@pedroh2604
Copy link

pedroh2604 commented Jul 29, 2020

Do you want to request a feature or report a bug?
Report a bug
What is the current behavior?
A populated object in a nested array is losing its population after I try to assign a new version to it

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

const mongoose = require('mongoose');
const uriFormat = require('mongodb-uri');

const ObjectId = mongoose.Schema.ObjectId;

// SCHEMAS
let StepSchema = new mongoose.Schema({
    ride: {
        type: ObjectId,
        ref: 'Ride',
        index: true
    },
    status: {
        type: Number,
        default: 0
    }
}, { usePushEach: true, timestamps: true });

let RideSchema = new mongoose.Schema({
    status: {
        type: Number,
        required: true,
        default: 0
    },
    steps: {
        taxi: [{
            type: ObjectId,
            ref: 'Step'
        }],
        rent: [{
            type: ObjectId,
            ref: 'Step'
        }],
        vehicle: [{
            type: ObjectId,
            ref: 'Step'
        }]
    }
}, { usePushEach: true, timestamps: true });

// MONGOOSE CONNECTION
let url = "";

const options = { 
    useNewUrlParser: true,
    useUnifiedTopology: true,
    useCreateIndex: true,
    retryWrites: false // using this due to our mLab production replicaSet mongo engine being MMAPv1
};

mongoose.Schema.Types.String.checkRequired(v => v != null);

function encodeMongoURI (urlString) {
    if (urlString) {
      let parsed = uriFormat.parse(urlString)
      urlString = uriFormat.format(parsed);
    }
    return urlString;
}

let mainDbConnection = mongoose.createConnection(encodeMongoURI(url), options);

mongoose.set('objectIdGetter', false);
mongoose.set('useFindAndModify', false);

let Ride = mainDbConnection.model('Ride', RideSchema);
let Step = mainDbConnection.model('Step', StepSchema);	

// LOGIC
(async () => {
	let ride = new Ride();
	try { ride = await ride.save();
	} catch (e) {return console.log('error saving ride', e);}

	let taxiStep = new Step({ride: ride._id});
	let vehicleStep = new Step({ride: ride._id});
	let rentStep = new Step({ride: ride._id});

	console.log('ride', ride);
	console.log('taxiStep', taxiStep);
	console.log('vehicleStep', vehicleStep);
	console.log('rentStep', rentStep);

	try { taxiStep = await taxiStep.save();
	} catch (e) {return console.log('error saving ride', e);}

	try { vehicleStep = await vehicleStep.save();
	} catch (e) {return console.log('error saving ride', e);}

	try { rentStep = await rentStep.save();
	} catch (e) {return console.log('error saving ride', e);}	

	ride.steps = {taxi: [taxiStep._id], vehicle: [vehicleStep._id], rent: [rentStep._id]};

	try { ride = await ride.save();
	} catch (e) {return console.log('error saving ride', e);}

	let foundRide;
	try { foundRide = await Ride.findOne({_id: ride._id}).populate({path:'steps.taxi steps.vehicle steps.rent', model:'Step'});
	} catch (e) { return console.log('mongo error', e); }

	console.log('foundRide.steps before', foundRide.steps); // e.g. foundRide.steps.taxi is an array of populated objects from the Step model

	foundRide.steps = doSomething(foundRide);
	console.log('foundRide.steps after', foundRide.steps); // e.g. foundRide.steps.taxi becomes an array of _id .Why does it lose population?
})();

const doSomething = ride => ride.steps;

What is the expected behavior?
It's expected for the nested array objects to keep its populated fields

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

@vkarpov15 vkarpov15 added this to the 5.9.27 milestone Jul 29, 2020
vkarpov15 added a commit that referenced this issue Aug 5, 2020
@vkarpov15 vkarpov15 added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Aug 5, 2020
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

2 participants