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

Getting an older paramter in mongoose because of a push #5407

Closed
DaWaD3 opened this issue Jun 27, 2017 · 1 comment
Closed

Getting an older paramter in mongoose because of a push #5407

DaWaD3 opened this issue Jun 27, 2017 · 1 comment
Milestone

Comments

@DaWaD3
Copy link

DaWaD3 commented Jun 27, 2017

Hi,

I am using the newest mongoose version (4.11.0) and node 6.11.0
I created a simple example where an animal should be added to my database. Therefor I am using th push-function.

When I check the database I expect an item with cat 'Zildjian' and an other cat 'Zohan' but the second one has both cats.

This seems to be a bug to me.

Can someone please help how to solve that problem?

Here is my code:

let mongoose = require('mongoose');

let catSchema = new mongoose.Schema({
                                      name: {type: String, required: true}
                                    });

let animalSchema = new mongoose.Schema({
                                         sound: {type: String, required: true},
                                         animals: {
                                           type: {
                                             cat: [catSchema],
                                           },
                                           required: true,
                                           default: {
                                             cat: [],
                                             dog: []
                                           }
                                         }
                                       });

exports.default = mongoose.model('animalSchema', animalSchema);
let mongoose = require('mongoose');

mongoose.connect('mongodb://test1/test');

let animalModel = require('./model').default;

function enterThings(house, name) {
  let animal = new animalModel({sound: house});
  animal.animals.cat.push({name: name});

  animal.save(function (err) {
    if (err)
      console.log(err);
    else
      console.log('Yay!');
  });
}

setTimeout(function() {
  enterThings('miaow', 'Zildjian')
}, 1000);

setTimeout(function() {
  enterThings('woof', 'Zohan')
}, 2000);

setTimeout(function() {
  process.exit(0);
}, 3000);
@vkarpov15
Copy link
Collaborator

You have a couple issues here. First, your schema declaration won't work as you expect. This schema:

let animalSchema = new mongoose.Schema({
                                         sound: {type: String, required: true},
                                         animals: {
                                           type: {
                                             cat: [catSchema],
                                           },
                                           required: true,
                                           default: {
                                             cat: [],
                                             dog: []
                                           }
                                         }
                                       });

is equivalent to this one:

let animalSchema = new mongoose.Schema({
                                         sound: {type: String, required: true},
                                         animals: {
                                           type: {}, // <-- "Mixed" type, means no type casting
                                           required: true,
                                           default: {
                                             cat: [],
                                             dog: []
                                           }
                                         }
                                       });

If the type property is a plain object, mongoose will interpret it as a mixed type, so no casting or change tracking.

Re: the duplicates, that's a bit of a weird gray area with the fix for #1380 . As a workaround you can do:

default: () => ({ cat: [], dog: [] })

But we'll put in a fix for that for the next patch release. The old behavior is still accessible via the shared property.

@vkarpov15 vkarpov15 added this to the 4.11.2 milestone Jul 4, 2017
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