These are chat archives for Automattic/mongoose

6th
Dec 2016
nick
@nick-ChenZe
Dec 06 2016 09:44 UTC

Hi, guys.I still have a problem after search on google. I can use pre.save hook, after model.save, but pre.update seems not working,
is something wrong in the way i use it, because pre.update is a query middleware not a doc middware. Fogive my poor englist. Thank you so much !!
maybe router.js

let UserModel = mongoose.model('User');
let id = xxxxx;
UserModel.update({_id: id}, some value, () => {
    some callback()
})

model.js

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

let UserSchema = new Schema({
    some props
});

let ItemModel = mongoose.model('User', UserSchema);

UserSchema.pre('update', function(next) {
    let self = this;
    console.log(111111111);
    // this not work !!! 
    next();
})
Yurij Molchanov
@daviatorstorm
Dec 06 2016 13:23 UTC
Hello guys. Please add new<TI>(doc?: TI): T to index.d.ts
Yurij Molchanov
@daviatorstorm
Dec 06 2016 13:30 UTC
very usefull thing.
U can split interfaces into two.
For example https://gist.github.com/daviatorstorm/9179642194ae04c38c932998fab1664d
Will White
@willnwhite
Dec 06 2016 15:11 UTC
Is it possible to send a function to your database to update data, rather than using the database's Update API?

Say if I had a

const doubleDonations = (doc) => ({ ...doc, doc.donations * 2})

,

Could I then send doubleDonations to the database like

Model.update({}, doubleDonations)

?

Will White
@willnwhite
Dec 06 2016 15:17 UTC
Not necessarily in MongoDB, just with any database.
Because it feels like I'm building my application's business logic into API calls to the database at the moment.
I wouldn't even know how to double, but say to increment I'm doing
Model.update({}, { $inc: { donations: 1 } })
Which is putting business logic (incrementing) into an API call.
What if I decide I need to use a different database in the future?
I'd rather not write my application around Mongoose and MongoDB, or any one database.
Will White
@willnwhite
Dec 06 2016 15:25 UTC
Perhaps I've missed a tutorial or two here? I'm just working from the Mongoose docs.
Bruno Watt
@delprofundo
Dec 06 2016 15:33 UTC
well a service layer above the model layer would tell the model to make these calls
Daniel Youberg
@dyouberg
Dec 06 2016 15:36 UTC

And if you want to double:

Model.update({}, { $mul: { donations: 2 } })

:P
Will White
@willnwhite
Dec 06 2016 15:37 UTC
What if I want to double then add one?
Two separate calls?
Daniel Youberg
@dyouberg
Dec 06 2016 15:38 UTC
I was being facetious, realistically you want to do this in your client/server function and then just use $set
Will White
@willnwhite
Dec 06 2016 15:38 UTC
Okay.

@delprofundo Like

const incrementDonations = (by) => Model.update({}, { $inc: { donations: by } })

?

Bruno Watt
@delprofundo
Dec 06 2016 15:42 UTC
yeah pretty much
Will White
@willnwhite
Dec 06 2016 15:44 UTC
Don't know why I'm having such a hard time with this! Are there some resources you can recommend?
Bruno Watt
@delprofundo
Dec 06 2016 15:44 UTC
ur API tier can then call thoses functions or functions that call those functions, it cares little what happens below.
ah yeah actually give me a minute
are you familar withthe concept of Service Oriented Architecture?
Will White
@willnwhite
Dec 06 2016 15:45 UTC
No. Think I've heard of it only.
Bruno Watt
@delprofundo
Dec 06 2016 15:45 UTC
ah
ok cool …one sec
martin fowler is your guy…hes a very good source on all things architecture and patterns
oh not that link sorry
actually its hard to find a good starting resource online, as SOA is a big topic. If your app is serving human clients and not other services then maybe start with MVC pattern as that achieves the same outcome, decoupling the data from the logic and from the view
Bruno Watt
@delprofundo
Dec 06 2016 15:51 UTC
but this is the book for the deep understaning of the patterns http://martinfowler.com/books/eaa.html
Will White
@willnwhite
Dec 06 2016 15:52 UTC
Thanks. Do you know of any MongoDB/Mongoose resources other than the docs?
Bruno Watt
@delprofundo
Dec 06 2016 15:54 UTC
I build API’s, sometimes I have many more layers
like a representor layer that uses a contract pattern to choose from a library of media types to return, this means its super easy to add a new flavour of the month media type when they come along
but at the end of the day however many layers you have so long as you keep the layers doing their own thing and no one elses ur doing it right
thats a great book on mongo patterns
packt has a mongoose book that covers some of the same cases
also try to use depedency injection to access the DB
makes testing much easier
Will White
@willnwhite
Dec 06 2016 16:03 UTC
Could you point me to the specific packt book? There are more than one.
Bruno Watt
@delprofundo
Dec 06 2016 16:03 UTC
mongoose for application development. i dont remember it being that helpful but I was quite familiar with most of the toplics
Will White
@willnwhite
Dec 06 2016 16:07 UTC
Okay thanks. I have a free book voucher with them so I might use it!
Bruno Watt
@delprofundo
Dec 06 2016 16:07 UTC
have a read of a sample if there is one. packt are hit and miss…ive had some great titles and others are just a slight repackaging of API documentation
Will White
@willnwhite
Dec 06 2016 16:08 UTC
Yeah same.
Will White
@willnwhite
Dec 06 2016 17:36 UTC
How do you update multiple documents in the db and return them (e.g. as an array) to your application?
I've read "If you want to update a single document in the db and return it to your application, use findOneAndUpdate instead [of update, which updates documents in the database without returning them]."
Daniel Youberg
@dyouberg
Dec 06 2016 17:48 UTC
Looks like Mongoose doesn't have findAndModify
Ahh that only returns one doc also
Will White
@willnwhite
Dec 06 2016 19:26 UTC
Right.
Guess I can update multiple then find multiple.