These are chat archives for Automattic/mongoose

16th
Mar 2018
Mihajlo Ilijić
@Pritilender
Mar 16 2018 08:44
@AngelMunoz do you have some index on company's name or location's name?
Kev
@lineus
Mar 16 2018 10:26
@Pritilender, the schema for company is the only one in the issue @AngelMunoz opened in the mongoose queue #6248, and it doesn't have an index.
Mihajlo Ilijić
@Pritilender
Mar 16 2018 10:28
@lineus he says that the Industry and Location have some unique fields, therefor they have unique index. But they are also subdocs, so no separate collection for them, and when he creates them on the fly, I think that the index on them gets violated.
I've edited the previous response because I realized how god damn awful my English can be from time to time...
Kev
@lineus
Mar 16 2018 10:33
are you kidding? your english is as good as mine and I've been in the US for 40+^w a long time.
Mihajlo Ilijić
@Pritilender
Mar 16 2018 10:34
Well, thanks :D
Anyhow, my guess for E11000 is unique index on name for Industry and state for Location schemas.
Kev
@lineus
Mar 16 2018 10:38
yeah, I didn't instinctively make the connection between unique and index. I'm still so new at this that unless I see the schema(s) in their entirety I'm pretty useless.
Mihajlo Ilijić
@Pritilender
Mar 16 2018 10:42
It's my assumption also, I agree that we should see @AngelMunoz 's schemas.
Brian Quinn
@brian-learningpool
Mar 16 2018 12:05
Hi everybody, I think I've found an issue where retainKeyOrder doesn't respect the order of nested objects/properties. This seems to have been introduced in version 4.6.4.
Kev
@lineus
Mar 16 2018 12:06
Hey @brian-learningpool, do you have a reproducible example?
Brian Quinn
@brian-learningpool
Mar 16 2018 12:06
It's the case all the way up to the 4.13.12
Hi Kev, I'll try to get one for you. I've noticed this in a business application and it'll take me a little while to unfurl it
Kev
@lineus
Mar 16 2018 12:41
here's a contrived example created for fun gist
@brian-learningpool ^
Kev
@lineus
Mar 16 2018 12:50
and I just realized that the || false is superfluous. I'm dense. fixed :P
of course, lol, I edited it in the gist and then re-ran my code locally to test the change. ( like I said, dense. )
Kev
@lineus
Mar 16 2018 13:37
also, I was setting the retainKeyOrder only on the toObject option, which only changes the javascript object. The schema wide retainKeyOrder option wasn't added until 4.6.4, (It defaults to false). previous versions would store the keys in reverse order by default.
Kev
@lineus
Mar 16 2018 13:44
#!/usr/bin/env node
'use strict'

const mongoose = require('mongoose')
const schema = new mongoose.Schema({})

console.log(`mongoose ver:  ${mongoose.version}`)
console.log(`retainKeyOrder: ${schema.get('retainKeyOrder')}`)
outputs on 4.6.4 and 4.6.3:
gitter: ./orderTest.js 
mongoose ver:  4.6.4
retainKeyOrder: false
gitter: npm install mongoose@4.6.3 >/dev/null 2>&1
gitter: ./orderTest.js 
mongoose ver:  4.6.3
retainKeyOrder: undefined
gitter:
Bruno Barros
@bybrunobarros
Mar 16 2018 14:50
Hey everyone! Is there something special to know about document attributes? Sometimes it seems I can refer to attributes like this someDoc.attr, but sometimes I get undefined and have to go through _doc like this someDoc._doc.attr. I don't use toObject(), it would be obvious if I did.
And attributes are not virtuals
Kev
@lineus
Mar 16 2018 15:22
@bybrunobarros are you talking about documents returned from a query?
Bruno Barros
@bybrunobarros
Mar 16 2018 15:23
@lineus Yes
SomeDoc.findById(id)
  .then(someDoc => {
    console.log(someDoc.attr) // doesn't work
    console.log(someDoc._doc.attr) // works
  })
with
const schema = new Schema({
  attr: String
})
mongoose.model('SomeDoc', schema)
Kev
@lineus
Mar 16 2018 15:28
there's an option called minimize that defaults to true that effect toObject() which console.log() is calling for you I think..
Bruno Barros
@bybrunobarros
Mar 16 2018 15:32
I don't think it's the subject. Or I don't understand what you're saying. My question is how should I access the document attributes? With or without _doc?
Kev
@lineus
Mar 16 2018 15:33
my answer applies only if the value of someDoc.attr isn't defined yet. is that path defined on someDoc?
Bruno Barros
@bybrunobarros
Mar 16 2018 15:34
Yes
Kev
@lineus
Mar 16 2018 15:36
are you selecting fields in your findOne call?
Brian Quinn
@brian-learningpool
Mar 16 2018 15:36
@lineus apologies for the late reply
Kev
@lineus
Mar 16 2018 15:37
@bybrunobarros something like findOne({_id: someid}, 'name')
Brian Quinn
@brian-learningpool
Mar 16 2018 15:37
That fixes it, I had some code setting refineKeyOrder on toObject() prior to 4.6.4. When I move it to the Schema instantiation it works as expected
Bruno Barros
@bybrunobarros
Mar 16 2018 15:37
This is more like this
SomeDoc.findById(id, 'attr attr1 attr2')
  .then(someDoc => {
    console.log(someDoc.attr) // undefined
    console.log(someDoc._doc.attr) // works
  })
Kev
@lineus
Mar 16 2018 15:38
oh right on @brian-learningpool
Brian Quinn
@brian-learningpool
Mar 16 2018 15:38
:+1:
Kev
@lineus
Mar 16 2018 15:42
hmm. if attr has a value then I can't see any reason why it wouldn't be returned
Bruno Barros
@bybrunobarros
Mar 16 2018 15:43
@lineus and here I am :)
It happens sometimes, I don't understand why. I probably do something wrong, but I can't figure out what.
Kev
@lineus
Mar 16 2018 15:44

@bybrunobarros consider a simple example like this one:

#!/usr/bin/env node
'use strict'

const mongoose = require('./lib/test_db')
const Schema = mongoose.Schema

const schema = new Schema({
  nick: String,
  name: {
    first: String,
    last: String
  },
  home: String
})

const User = mongoose.model('users', schema)

const billy = new User({
  nick: 'Blucifer',
  name: {
    first: 'William',
    last: 'Morningstar'
  }
})

billy.save((err, doc) => {
  if (err) { return console.error(err) }
  return console.log(doc)
})

outputs:

{ name: { first: 'William', last: '' },
  _id: 5aabe37b68071abfb5fcd8ac,
  nick: 'Blucifer',
  __v: 0 }
I didn't set home, so it didn't appear in the output
if I set home:
{ name: { first: 'William', last: 'Morningstar' },
  _id: 5aabe6b6614d0fc1070c2d87,
  nick: 'Blucifer',
  home: 'Hades',
  __v: 0 }
Bruno Barros
@bybrunobarros
Mar 16 2018 15:52
@lineus Thanks a lot. Once again I forgot. My model is in a package, I'm used to link it when I work on something related to it. But I installed an older version by mistake.
So everything you said make sense now :)
Kev
@lineus
Mar 16 2018 15:53
I can't count the number of times I've done that @bybrunobarros :)
Bruno Barros
@bybrunobarros
Mar 16 2018 15:53
I can't count the number of hours I've lost because of it :worried:
Kev
@lineus
Mar 16 2018 15:58
speaking of lost time, I'm off to the treadmill.