These are chat archives for Automattic/mongoose

29th
Mar 2018
Ghost
@ghost~5928d90bd73408ce4f629b9e
Mar 29 2018 04:35
But both of them are working so what's the difference?
Chris Hurst
@ytsruh
Mar 29 2018 04:37
@2HexGFX_twitter the first one you posted is just a plain JS object built on the normal protype, so it only has the normal JS protype properties. The second one extends the JS prototype with the schema with the additional properties that mongoose gives it. Whilst you might not need to use them right now, you may need them in the future
Ghost
@ghost~5928d90bd73408ce4f629b9e
Mar 29 2018 04:39
oh a right that makes sense thank you for clarification
Kev
@lineus
Mar 29 2018 07:53

@2HexGFX_twitter I hadn't ever tried using mongoose.model with a POJO until now. I didn't realize that mongoose.model creates an instance of Schema for you if you pass in an object literal.
from mongoose/lib/index.js Mongoose.prototype.model:

  if (utils.isObject(schema) && !(schema.instanceOfSchema)) {
    schema = new Schema(schema);
  }

so I was wrong above, you can just use object literals with mongoose.model(), which gives you SchemaTypes and all that comes with them. Things like static and instance methods, custom query helpers, hooks and virtuals are all added to the schema with methods that require you to call new Schema for yourself though.

Kev
@lineus
Mar 29 2018 08:05

@2HexGFX_twitter is that what you meant by

both of them are working

Ghost
@ghost~5928d90bd73408ce4f629b9e
Mar 29 2018 10:21
yes
so creating an instance vs using object literal and passing it on mongoose.model accomplish the same thing got it
Kev
@lineus
Mar 29 2018 12:26
@2HexGFX_twitter it's kind of like choosing to ride a fixed gear bike in San Francisco. You'll find yourself pushing your bike up a few hills that you'd otherwise be able to ride up without a problem. It will either bother you or it won't :)
Kirk Sefchik
@deathgaze
Mar 29 2018 16:21
is there a way to get a count from a cursor?
Kev
@lineus
Mar 29 2018 16:42
@deathgaze do you mean how many total documents it's going to eventually return? or the current length of the queue?
Kirk Sefchik
@deathgaze
Mar 29 2018 17:26
@lineus total docs. but I think I found answer: query.count().then(count => ...
Is that correct?
Kev
@lineus
Mar 29 2018 17:31
yeah, that will work on a query, not sure about an instance of a QueryCursor though (if I understood you correctly before that you're working with the readable Stream interface of Query.cursor() )
Kirk Sefchik
@deathgaze
Mar 29 2018 18:34
@lineus luckily count doesn't take super long on huge document sets, which is super great and exactly what I was looking for :)