Skip to content
This repository

Why doesn't MongooseArray pop or shift produce a _delta()? #821

Closed
timoxley opened this Issue · 8 comments

2 participants

Tim Oxley Aaron Heckmann
Tim Oxley

If i have an array in my schema

var assert = require('assert')
var mongoose = require('mongoose')
var Schema = mongoose.Schema
mongoose.connect('mongodb://localhost/mongoosearraysettertest ')

var DummySchema = new Schema({
  name: String,
  items: [Number]
})

DummySchema.pre('save', function(next) {
  var delta = this._delta()
  console.log('pre save delta', delta) // prints delta if available
  next()
})

var Dummy = mongoose.model('Dummy', DummySchema)

Dummy.create({
  items: [1,2,3,4,5]
}, function(err, d) {
  assert.ifError(err)
  d.items.pop() // does not create delta??
  d.save(function(err, d) {
    assert.ifError(err)
  })
})

The presave produces "pre save delta undefined"

Same happens with shift but not push, $shift or $pop. Why is that? Seems inconsistent. If this is a bug, I'd be willing to fix it.

Also, I''m analysing _delta() to know what elements are added/removed from an array, is this safe (considering it's a _private method)? Is there another way to get the delta?

Aaron Heckmann
Collaborator

yeah its a bug. please submit a patch.

Tim Oxley

Moved onto a different project temporarily, patch will come… but keen to know if you have an answer to the second part of the question, @aheckmann :

Also, I''m analysing _delta() to know what elements are added/removed from an array, is this safe (considering it's a _private method)? Is there another way to get the delta?

Thanks man

Aaron Heckmann
Collaborator

its safe and there is no better way unless you hook the markModified method and watch the paths but even then you don't see the values so its pretty pointless.

Tim Oxley

Awesome, thanks heaps.

Tim Oxley

@aheckmann I don't actually understand what the $ prefix is supposed to mean vs no $, other than to provide consistency with the mongodb operations. In particular, I don't understand why we have:

MongooseArray.prototype.$push =
MongooseArray.prototype.push = function () {

but not:

MongooseArray.prototype.pop =
MongooseArray.prototype.$pop = function () {
Aaron Heckmann
Collaborator
Tim Oxley timoxley referenced this issue from a commit
Commit has since been removed from the repository and is no longer available.
Aaron Heckmann
Collaborator

I think this is fixed now right?

Tim Oxley

Yes, the issue was that _registerAtomic wasn't being called for any regular array operations other than push, the 'new' aliases force any regular push, pop, shift, unshift interaction with the array to go via mongoose.

Tim Oxley timoxley closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.