These are chat archives for Automattic/mongoose

4th
Apr 2016
Emanuele DelBono
@emadb
Apr 04 2016 14:21
Hi. Is there an "internal documentation" of mongoose? Or, better, can someone point me to the piece of code?
I'm curious to know how it implements the model properties. If I log a model it does't have the plain properties defined in the schema (they are in a nested-object _doc). But if I write console.log(modelInstance.myProp) it writes the value contained in the object_doc. How is it implemented? :)
Pier-Luc Gagnon
@Nepoxx
Apr 04 2016 17:02
Well, it's open source
Let me see if I can find it
Emanuele DelBono
@emadb
Apr 04 2016 18:15
@Nepoxx yes, I know, but it's more than 3k lines. I was wandering if someone have more precise information. :smile:
Algram
@Algram
Apr 04 2016 19:02

I have a collection with objects that look like this: http://pastebin.com/JGj3SMKc
Every now and then I get a list of new videos and I want to add them to the videos array. But also some of the existing videos get different date ("videoCount" changes) and I want to update this field too. On top of that, the channel also get's some updated data ("avgVideoViews"). So how do I update all of that? I would be very glad if someone could help me out, I tried the regular way with the update()-function, but I hit a wall at some point because it got too messy and I just dont know how to update that kind of thing.

Thanks for your help!

Pier-Luc Gagnon
@Nepoxx
Apr 04 2016 19:03
Fetch the document, modify it, then save it
It's not atomic, but it's the right way to do it
wujekkane
@wujekkane
Apr 04 2016 19:03
Hello, any idea how to save datetime from input in mongoose without errors?
Algram
@Algram
Apr 04 2016 19:04
@Nepoxx But that would be the reason why the right thing to do would be to properly update the thing. Isn't there a way to use the proper updating toolkit?
Pier-Luc Gagnon
@Nepoxx
Apr 04 2016 19:05
Well it's a mentality thing
Application logic should be in the application
Algram
@Algram
Apr 04 2016 19:05
In my mentality I want to update and not replace. Also the logic will be in the application, inside the channelController.js
Pier-Luc Gagnon
@Nepoxx
Apr 04 2016 19:06
And update is performed on the database, not in the application
I'm not saying it's right or wrong, I'm saying that's the mentality behind it
That's one of the reasons why Mongoose has versioning
Algram
@Algram
Apr 04 2016 19:06
@Nepoxx also if I just replaced: It can happen that the new channel object does not have all the videos the old object has. So in that case I would lose the old videos and I dont want that
Pier-Luc Gagnon
@Nepoxx
Apr 04 2016 19:06
I'm not telling you to replace it
Algram
@Algram
Apr 04 2016 19:06
I know, just elaborating the problems
Pier-Luc Gagnon
@Nepoxx
Apr 04 2016 19:07
You should do MyModel.find(yourQuery, ...), then modify that object in your application, then re-save it
The update API has a few things to help you out, like $push, $inc, etc.
Algram
@Algram
Apr 04 2016 19:08
@Nepoxx so are you saying use the update api or dont?
Pier-Luc Gagnon
@Nepoxx
Apr 04 2016 19:08
You've got a good set of documentation about it here: https://docs.mongodb.org/manual/reference/command/findAndModify/
If it's simple enough, I say use the update API, it's one lesss query. If it gets too complex, do a find then a save
Valeri Karpov
@vkarpov15
Apr 04 2016 19:26
@wujekkane can you clarify your question please?
@Algram depends on your use case. Mongoose save() is designed primarily for things like forms, where you have heavy validation requirements but don't care too much about concurrency - if the user submits the same form twice, no big deal, you get the same data. If you have a use case where you care about concurrent requests, may be better to use update or findOneAndUpdate
wujekkane
@wujekkane
Apr 04 2016 19:31
@vkarpov15 I was thinking it needs to be parsed somehow. Turned out I mispelled variable name ;)
Vlado Tesanovic
@vladotesanovic
Apr 04 2016 19:32
@vkarpov15 Did you have time to try Mongoose with Type Script, especially with async - await implementation? In background it is resolved to Promises, so we must use exec() method. Code looks very clean...
Algram
@Algram
Apr 04 2016 19:34
@vkarpov15 yeah you are correct. I managed to use .update now. I only have one problem left: If I have a video on the old data and not in the new data, it get's removed with the update. Is there an option to "disable" that?
Vlado Tesanovic
@vladotesanovic
Apr 04 2016 19:34
@vkarpov15 I read once from you, that if you need multiple callbacks you are doing something wrong... But this promises ( async - await looks very neat )

schema.static("combine", async function () {
   const data = await this.find({}).exec();

   return Promise.resolve({
      _id: 2,
      _inner: data
   });
});
Felipe Vargas
@fvargas
Apr 04 2016 20:00
I'm new to mongoose, and I'm wondering what packages are popular/recommended for performing validation?
Also, my understanding is that the pluggable ways to perform validation would be using pre(), post(), or custom validators. Is this correct, and if so which form is typically used by packages?
i.e. pre()/post() vs. custom validators?