These are chat archives for Automattic/mongoose

Jun 2017
Paul "Joey" Clark
Jun 19 2017 00:54

@teogenesmoura return election and return voter do not do what you want.

You can't return to the outer code because the DB queries are asynchronous (take time).

The solution is to not return, and instead put your later code inside the callback. This produces ugly nested indented code, which is the Javascript way. :P

app.get('___', function (req, res, next) {
    // calling next with one argument tells express there was an error

    Election.findOne({ 'name' : req.body.electionName }, function(err, election ){
        if(err) return next(err);
        Voter.findOne({ 'name' : req.body.voterName }, function(err, voter) {
            if(err) return next(err);
            election.update({ $push: { 'voters': voter }}, function(err, result) {
                if(err) return next(err);

Paul "Joey" Clark
Jun 19 2017 01:02

Using .then() instead of a callback can help reduce the indentation a bit.

Using async-await can help reduce it a lot.

Paul "Joey" Clark
Jun 19 2017 03:15
@AdelMahjoub has a good point too, that findOne() is not guaranteed to provide a valid election or voter if the client's request contains bad or out-of-date names.
Julio Borja Barra
Jun 19 2017 06:26
I have this
var GroupSchema = new Schema({
  active: { type: Boolean, default: true },
  description: [{ name: String, lang: String }],
  categories: [{
    type: Schema.Types.ObjectId,
    ref: 'Category'
  title: [{ name: String, lang: String}],
  created_at : { type : Date, default : },
  updated_at : { type : Date, default : },
  created_by: { type: Schema.Types.ObjectId, ref: 'User' }
const CategorySchema = new Schema({
  color: String,
  groups : [{
    type: Schema.Types.ObjectId,
    ref : 'Group'
and would like to search for all the groups where a category's color matches 'red'. ¿is this possible?
Jun 19 2017 07:50
it seems can use populate api
Jun 19 2017 10:24
$ mongod
2017-06-19T15:50:13.232+0530 I CONTROL  [initandlisten] MongoDB starting : pid=6696 port=27017 dbpath=C:\data\db\ 64-bit host=COMP-4
2017-06-19T15:50:13.233+0530 I CONTROL  [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2
2017-06-19T15:50:13.233+0530 I CONTROL  [initandlisten] db version v3.4.5
2017-06-19T15:50:13.233+0530 I CONTROL  [initandlisten] git version: 520b8f3092c48d934f0cd78ab5f40fe594f96863
2017-06-19T15:50:13.233+0530 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.1u-fips  22 Sep 2016
2017-06-19T15:50:13.233+0530 I CONTROL  [initandlisten] allocator: tcmalloc
2017-06-19T15:50:13.233+0530 I CONTROL  [initandlisten] modules: none
2017-06-19T15:50:13.233+0530 I CONTROL  [initandlisten] build environment:
2017-06-19T15:50:13.233+0530 I CONTROL  [initandlisten]     distmod: 2008plus-ssl
2017-06-19T15:50:13.233+0530 I CONTROL  [initandlisten]     distarch: x86_64
2017-06-19T15:50:13.233+0530 I CONTROL  [initandlisten]     target_arch: x86_64
2017-06-19T15:50:13.233+0530 I CONTROL  [initandlisten] options: {}
2017-06-19T15:50:13.233+0530 I STORAGE  [initandlisten] exception in initAndListen: 29 Data directory C:\data\db\ not found., terminating
2017-06-19T15:50:13.233+0530 I NETWORK  [initandlisten] shutdown: going to close listening sockets...
2017-06-19T15:50:13.233+0530 I NETWORK  [initandlisten] shutdown: going to flush diaglog...
2017-06-19T15:50:13.233+0530 I CONTROL  [initandlisten] now exiting
2017-06-19T15:50:13.233+0530 I CONTROL  [initandlisten] shutting down with code:100
Paul "Joey" Clark
Jun 19 2017 10:55
Data directory C:\data\db\ not found <— @theromie
Paul "Joey" Clark
Jun 19 2017 11:00

@juboba Mongo isn’t really designed to do joins.

Some people get around it by putting copies of the data they need into the documents that need them, to make queries smoother. (That means denormalized data and more complicated updates.)

People using populate seem to complain that it filters the populated docs, but doesn’t actually filter the parent docs!

But if you succeed, I’d love to see how. :)

Paul "Joey" Clark
Jun 19 2017 11:08
One approach might be to query in two steps:
  • First find all the Categories with {color: ‘red’}, and gather their _ids in an Array.
  • Then find all the Groups with {categories: {$in: matchingCategoriesArray}}
Jun 19 2017 11:12
Is there a neat way to join two queries like lodash _.defaults? I have a set of defaults in db, and user can override from their settings in db. When calling the settings I'd like to return the defaults combined with overrides as one result. Curious how to do this properly. Any suggestions? Thanks muchly!
Using mean structure so express is the server side, if that helps.
Paul "Joey" Clark
Jun 19 2017 11:15
_.defaults() sounds like it might be an early version of Object.assign()
const query = Object.assign({}, defaultQuery, givenQuery);
It will only fill properties missing at the top level though @dougal83, not deeply.
Jun 19 2017 11:20
Sounds like a good place to start, I'll go read up on it. Thank you for your advice! @joeytwiddle
Jun 19 2017 11:24
@joeytwiddle i have created directory
now working
Paul "Joey" Clark
Jun 19 2017 11:32
Gene N.
Jun 19 2017 13:59
Hi are there any guides / examples on how to create / update sub documents in a mongoose model via url-encoded (postman) ?