These are chat archives for Automattic/mongoose

7th
Jun 2017
Joshua Austill
@jlaustill
Jun 07 2017 00:32
@awb715 passport is amazing in my experience.
Aaron Bell
@awb715
Jun 07 2017 07:39
im a fairly new developerbut i want to create unique user accounts and store user data ina db
@sulthonzh
@jlaustill
Peter Delvaux
@pjetr
Jun 07 2017 10:04

hi guys, question.
I have a model that I modify using the pre-save hook in the schema. When saved, mongoose returns the modified data, __v is updated and everything looks dandy. But when I retrieve the model, my model isn't changed at all...

var schema = new Schema({
  name: {
    type: String,
    required: true,
  },
  version: {
    type: Number,
    required: false,
  },
  versions: {
    type: Object,
    required: false,
  },
  elements: {
    type: Array,
    required: false,
  },
})
  .pre('save', function(next) {
    version = Object
      .keys(this.versions)
      .map(function(key) { return parseInt(key); })
      .sort(function(a, b) { return a - b; })
      .reverse()[0] + 1;
    if (isNaN(version)) {
      version = 1;
    }

    this.versions[version] = {
      createdAt: new Date(),
      elements: this.elements.slice(),
    };
    this.elements = null;
  })

This is a slimmed down version of what I've got

If I update the name, that change is saved to my database
I need to be able to return to previous versions of my saved document and it's children.
Peter Delvaux
@pjetr
Jun 07 2017 10:20
I've turned this into a stackOverflow question as well: https://stackoverflow.com/questions/44409883/modifing-in-a-pre-save-hook-not-persisted
Aaron Bell
@awb715
Jun 07 2017 15:22
@nsarafa id like to here more about your experience, i plan to use passportnin the near future
Дмитрий
@CatBegemot
Jun 07 2017 17:25
hello, who can help me?
I want to find all objects where the "parent" key is not set
db.folders.find()
{ "_id" : ObjectId("593832a497a1ae203450daee"), "path" : "593832a497a1ae203450daee", "name" : "Folder", "__v" : 0 }
{ "_id" : ObjectId("593832ae97a1ae203450daef"), "path" : "593832a497a1ae203450daee#593832ae97a1ae203450daef", "parent" : ObjectId("593832a497a1ae203450daee"), "name" : "SubFolder", "__v" : 0 }
first object for example
Joshua Austill
@jlaustill
Jun 07 2017 18:01
@CatBegemot you are looking for "parent" : { $exists: false} , see https://docs.mongodb.com/manual/reference/operator/query/exists/
Дмитрий
@CatBegemot
Jun 07 2017 18:02
@jlaustill, yes, thnx i find))