These are chat archives for LearnBoost/mongoose

26th
Jan 2015
Ilan Biala
@ilanbiala
Jan 26 2015 00:49
@chetverikov I'll have to do some investigation, because it seems that Date objects do work, but the tests don't make that clear I think.
Is there any reason why .save() and .update() cannot be called through .exec()?
Valeri Karpov
@vkarpov15
Jan 26 2015 01:52
.update() can, but .save() is not tied in to the query builder, so doesn't have an exec()
Ilan Biala
@ilanbiala
Jan 26 2015 03:25
Do all the queries not inherit from a query builder class or something like that? I'm just curious, because it gets confusing sometimes and I think some parts of Mongoose could be simpler than they currently are, but I haven't tested 4.0.0 yet, if that brings any syntax changes.
Maksim
@chetverikov
Jan 26 2015 03:27
@ilanbiala Hmm… That seems confusing to you?
Ilan Biala
@ilanbiala
Jan 26 2015 03:36
Well there are more queries than just save and update, find, findById, findAndUpdate, etc. so I think it would make sense for every Mongoose query to have the same API, no?
Maksim
@chetverikov
Jan 26 2015 03:39
@ilanbiala honestly, I don't understand what you mean. +) Can I seldom use the Query Builder
Ilan Biala
@ilanbiala
Jan 26 2015 03:40
Shouldn't every Mongoose query have pretty much the same API?
Maksim
@chetverikov
Jan 26 2015 03:42
I've never tried to do complex issues through the Query Builder. I always use the find* methods are simpler and more obvious.
Maksim
@chetverikov
Jan 26 2015 03:51

@vkarpov15

If we talk about logic, then I would suggest to make all the find* methods in the collection for 4.0 version. That is to share the essence of their purpose. Example:

mongoose.collect(‘Places’).find({}).exec()
  .then(function( results ){
    results // is Collection object
    results.toObject() // call toObject at each of the documents

    results[0] // is Document
  })

This separation will make clear in working with mongoose. Explanation of terms:

Collection

  • contains a method to retrieve the data and work with data collection. A collection can contain multiple schemas (similar to the discriminator). Each collection can contain your adapter (connection), this will allow easy to do client-server

Model

  • is, roughly speaking, the Document constructor. May contain methods create, statics and other…

Document

  • is filled model (new Model( dataFromDBorRequest )). May contain methods save, update, path and custom methods from schema…
connect -> (many) Collection -> (many) Schema
                             -> (many) Models
Valeri Karpov
@vkarpov15
Jan 26 2015 14:44
.save() isn't really a query in mongoose. It's intended to be a member function on a document that may execute an update or an insert depending on context. Queries (find, findOne, update, etc.) are attached to models, and go through a common query builder class.