Skip to content

Value pushed to array not persisted #7889

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
jyrkive opened this issue Jun 12, 2019 · 0 comments · Fixed by #7891
Closed

Value pushed to array not persisted #7889

jyrkive opened this issue Jun 12, 2019 · 0 comments · Fixed by #7891
Milestone

Comments

@jyrkive
Copy link
Contributor

jyrkive commented Jun 12, 2019

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

Bug

What is the current behavior?

If you push a value to an array, populate the said array, push nothing to the said array, and finally save the document, the originally pushed value is not persisted to the database.

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

const mongoose = require("mongoose");
mongoose.connect("mongodb://localhost:27017/array-empty-push", {useNewUrlParser: true});

console.log(`Mongoose ${mongoose.version}`);

const Car = mongoose.model("Car", new mongoose.Schema({
  model: Number
}));

const Player = mongoose.model("Player", new mongoose.Schema({
  cars: [{type: mongoose.Schema.Types.ObjectId, ref: "Car"}]
}));

async function test() {
  await Player.deleteMany({});
  await Car.deleteMany({});

  let car = await Car.create({model: 0});
  await Player.create({cars: [car._id]});
  let player = await Player.findOne({});
  await player.populate("cars").execPopulate();

  console.log(player.cars.length);

  car = await Car.create({model: 1});
  player.cars.push(car);
  await player.populate("cars").execPopulate();

  car = await Car.create({model: 2});
  player.cars.push(car);

  console.log(player.cars.length);

  await player.save();
  player = await Player.findOne({});

  console.log(player.cars.length);

  mongoose.disconnect();
}

test();

What is the expected behavior?

The three numbers this script should print are 1, 3, 3. The actual values it prints are 1, 3, 2.

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

  • Node.js 12.4.0
  • Mongoose 5.5.14
  • MongoDB driver 3.2.7
  • MongoDB 3.4.6
@jyrkive jyrkive mentioned this issue Jun 12, 2019
jyrkive added a commit to jyrkive/mongoose that referenced this issue Jun 12, 2019
jyrkive added a commit to jyrkive/mongoose that referenced this issue Jun 12, 2019
jyrkive added a commit to jyrkive/mongoose that referenced this issue Jun 12, 2019
@jyrkive jyrkive mentioned this issue Jun 12, 2019
jyrkive added a commit to jyrkive/mongoose that referenced this issue Jun 12, 2019
jyrkive added a commit to jyrkive/mongoose that referenced this issue Jun 12, 2019
jyrkive added a commit to jyrkive/mongoose that referenced this issue Jun 12, 2019
jyrkive added a commit to jyrkive/mongoose that referenced this issue Jun 12, 2019
vkarpov15 added a commit that referenced this issue Jun 12, 2019
@vkarpov15 vkarpov15 added this to the 5.5.15 milestone Jun 12, 2019
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

Successfully merging a pull request may close this issue.

2 participants