These are chat archives for Automattic/mongoose

24th
Aug 2016
Andrei Demian
@unDemian
Aug 24 2016 08:46
hey guys, is there a way to set the readPreference so that it reads from secondary but writes to primary in a replica set?
I keep getting Mongo Error: not master if I use secondaryPreffered for the findAndUpdate
Felix Itzenplitz
@cebor
Aug 24 2016 11:22
hi, does the type Date in a Schema, only accepts a timeStamp (Date.now()), or does new Date() also work ?
find nothing in the docs
Jerome Sta. Maria
@stamariajerome
Aug 24 2016 15:06
Hi guys, How do you guys structure your users schema? especially if it has multiple user types(ex. users, admin, superadmin, etc)
Joël Poulin
@poulinjoel
Aug 24 2016 20:55
Hello everybody, I want to update (push) a new object in an array of objects, i know about .findOneAndUpdate(), but I'm not sure about which options to take and how to write it.
LeonineKing1199
@LeonineKing1199
Aug 24 2016 21:01
Use $push!
Joël Poulin
@poulinjoel
Aug 24 2016 21:02
@LeonineKing1199 Yea bro but where
LeonineKing1199
@LeonineKing1199
Aug 24 2016 21:02
Ah, I see what you mean.
In your update object
Joël Poulin
@poulinjoel
Aug 24 2016 21:02
Model.findOneAndUpdate(query, { type: 'slow' }, {upsert:true, new: true},function(err, result){
if(err) console.error(err);
i use findOneandUpdate, my query is defined
LeonineKing1199
@LeonineKing1199
Aug 24 2016 21:03
Well, look at this basic example from the link I posted:
db.students.update(
   { _id: 1 },
   { $push: { scores: 89 } }
)
In your example, the { type: 'slow' } part is likely where you'd write your update query.

query.findOneAndUpdate(conditions, update, options, callback) // executes

This is from the mongoose site.

So basically, you write your conditions object as your query
And then update is an object that likely will read:{ $push: { arrayName: valueToPush } }
Joël Poulin
@poulinjoel
Aug 24 2016 21:06
@LeonineKing1199 Imma give it a shot
LeonineKing1199
@LeonineKing1199
Aug 24 2016 21:06
Kewl. Check out this example as well
Joël Poulin
@poulinjoel
Aug 24 2016 21:11

@LeonineKing1199 Bro,

Bro, it worked thank you so much

LeonineKing1199
@LeonineKing1199
Aug 24 2016 21:11
Yay!
Joël Poulin
@poulinjoel
Aug 24 2016 21:12
General thought, I'm never sure how to construct my queries, if the property has to be wrapped in a string or not and if the value of this property has to be wrapped as well in a string.
@LeonineKing1199 I don't know if you know what I mean
LeonineKing1199
@LeonineKing1199
Aug 24 2016 21:13
How do you mean? Most of my query objects wind up looking like regular JSON blobs.
For example,
Member.find({ memberId: { $gt: 9000 }})
Looks for all members with a memberId greater than 9000
Joël Poulin
@poulinjoel
Aug 24 2016 21:17
@LeonineKing1199 so never use "memberId" or for example, you create a new Object giving it a state and a behavior (var object = new Object({state: 'state'}) ), can you use it in your query like {memberId: object} or {memberId:"object"}
LeonineKing1199
@LeonineKing1199
Aug 24 2016 21:17
Well, you can use something like "memberId" but I've found that just memberId is sufficient.
Also, it's "weird" in JS to explicitly call new Object
You can do it but I've hardly seen anyone do so. Instead, just use the literal syntax and just start typing { }
Now, as for { memberId: "1234" } vs { memberId: 1234 }, I try to match types as is defined in my schema
Joël Poulin
@poulinjoel
Aug 24 2016 21:19
@LeonineKing1199 Okok, but it was an example with a mongoose model
LeonineKing1199
@LeonineKing1199
Aug 24 2016 21:19
By that same token, it's also recommended that you never call new Array either.
Things like new Date() are typically accepted though.
Joël Poulin
@poulinjoel
Aug 24 2016 21:20
@LeonineKing1199 Ok thank you so much, I appreciate it
LeonineKing1199
@LeonineKing1199
Aug 24 2016 21:20
And if you must construct one of the primitives like String, Number or Boolean, make sure you use them like this: new Number('1234').valueOf() to get proper type coercions
typeof new Number('1234').valueOf() === 'number'
Where as
typeof new Number('1234') !== 'number'
Joël Poulin
@poulinjoel
Aug 24 2016 21:24
typeof new Number('1234') is a Number type right?
LeonineKing1199
@LeonineKing1199
Aug 24 2016 21:25
Well, new Number('1234') instanceof Number === true
But typeof new Number('1234') === 'object'
Even better, you can simply just call const myNumber = Number('1234')
You should play around in browser's console a bit and see what happens.
Joël Poulin
@poulinjoel
Aug 24 2016 21:30
typeof myNumber == 'number'