These are chat archives for Automattic/mongoose

6th
Mar 2018
Loganathan Velsamy
@loganathanav
Mar 06 2018 11:35
will mongoose support connecting MongoDB Atlas cluster?
Kev
@lineus
Mar 06 2018 11:36
@loganathanav yes :)
Loganathan Velsamy
@loganathanav
Mar 06 2018 11:37
I'm getting exception while trying to connect Atlas using mongoose.connect
@lineus did you ever tried to connect atlas using mongoose?
Kev
@lineus
Mar 06 2018 11:38
nope, but I've seen other folks mention that they use it.
what error are you seeing?
Loganathan Velsamy
@loganathanav
Mar 06 2018 11:40
@lineus when I try insert a new document i got error: "MongoError: not authorized on admin to execute command"
"MongoError: cannot do raw queries on admin in atlas" got this while trying to access get records without creating collection
Kev
@lineus
Mar 06 2018 11:44
is your mongodb uri string pointing to /admin ?
Loganathan Velsamy
@loganathanav
Mar 06 2018 11:44
yes
mongodb+srv://adminuser:mypassword@espiralcluster-98nmg.mongodb.net/myDb
Mihajlo Ilijić
@Pritilender
Mar 06 2018 11:52
@loganathanav If you're connecting to mongo atlas, the connection string should look something like this mongodb://adminUsername:adminPassowrd@cluster0-shard00.mongodb.net:27017,cluster0-shard01.mongodb.net:27017,cluster0-shard02.mongodb.net:27017/test?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin
notice that /test is the name of the DB you're connecting to
Kev
@lineus
Mar 06 2018 11:52
sorry, I had to put my youngest on the school bus.
I'm back
Mihajlo Ilijić
@Pritilender
Mar 06 2018 11:54
@loganathanav sorry, I now see that this is driver 3.6 string version
You should check your user if it's not just read-only, or maybe it has read/write privileges only on one db and you're connecting to the other.
You can check it in "Security" tab. You'll the roles of all the users, so just find the one you're using and see what it has set as his role.
Mihajlo Ilijić
@Pritilender
Mar 06 2018 11:59
Also check this SO and try adding authSource=admin as query param to the connection string: https://stackoverflow.com/questions/38237663/mongo-atlas-connection-authentication-failed-with-custom-databases
julian libor
@Linux249
Mar 06 2018 12:11
Hi all, i found an issue and i cannot name it propaly to find help but i cannot image that i'm the first how run in to this. Maybe you can help me :)
I have a schema like this one:
const UserSchema = new Schema({
    name: String,
    someValue: [{
        value: String
    }]
})
than i save some user and change the schema to this
```
const UserSchema = new Schema({
    name: String,
    someValue: {
        value: String
    }
})
(i removed the array behind 'someValue')
If i now load a user the user still has the attribute someValue: []
and if i want to add sameValue.value = "lalala" and save it i get an error
julian libor
@Linux249
Mar 06 2018 12:16
MongoError: Cannot create field 'value' in element {someValue: []}
maybe someone here knows this error?
Kev
@lineus
Mar 06 2018 12:21
changing a schema won't have any effect on existing documents in the db.collection. if you're find*()ing existing users in the collection they'll be as they were when you inserted them.
julian libor
@Linux249
Mar 06 2018 12:26
But the schema schould not load stuff from the db if it isn't in the schema, right?
julian libor
@Linux249
Mar 06 2018 12:32
Or is there a way to prevent loading 'old' stuff, which is not in the schema anymore?
Kev
@lineus
Mar 06 2018 12:39
you can use the $type operator in your query. docs here if you need help drafting a query to exclude the docs where someValue is an array I can help.
julian libor
@Linux249
Mar 06 2018 12:43
thanks - i think i can solve the problem with "load all docs from all collections, create for every doc a new doc (with the old id) and save everything"
Kev
@lineus
Mar 06 2018 12:44
just curious, is this a production database?
julian libor
@Linux249
Mar 06 2018 12:45
now its in dev mode
julian libor
@Linux249
Mar 06 2018 12:54
changing the schema and than just load a doc and save it should not cause an error in my opinion
i have a similar problem if i rename sameValue to samevalue, than i always gotthe old/first one returned.
but maybe i should open a issue because of this topic?
Kev
@lineus
Mar 06 2018 12:57
i was thinking about that... There might be an option I remember seeing for that. I'll grok the docs and see if I can find something.
julian libor
@Linux249
Mar 06 2018 12:58
thanks, the only one i found usefull was 'strict' but this enabled by default allready - hope you find something more :)
Kev
@lineus
Mar 06 2018 13:01
I was thinking of strictQuery, but that would only work if your new String path had a different name. sorry :(
julian libor
@Linux249
Mar 06 2018 13:02
thanks for your help :)
then i will open an issue soon
Kev
@lineus
Mar 06 2018 13:03
but, you could prevent the someValue field from showing up on your update query, and add a new field with a string instead to all.
any way you slice it, you've got your work cut out for you
Kev
@lineus
Mar 06 2018 13:08
const UserSchema = new Schema({
  name: String,
  someNewValue: String // or whatever you want
}, { strictQuery: true })
theoretically, that would cause the path someValue to be stripped from all found docs. The thing I'm curious about, is whether the field would still exist in the doc that gets saved during the update. I'm going to test and see.
for posterity, my guess is it won't, at least thats what I'm hoping for.
Loganathan Velsamy
@loganathanav
Mar 06 2018 13:40
@Pritilender Hey, that works! thanks
Mihajlo Ilijić
@Pritilender
Mar 06 2018 13:41
@loganathanav No problem :)
julian libor
@Linux249
Mar 06 2018 14:03
@Kev - everything that was saved seems to be still there, i could do anything to the schema, in the doc it remains
i change the schema, i load a user, he has still the old properties, i create a new user with the old stuff, this one "loose" the properties (cause they are not in the schema anymore), the newly created user gets the id from the old user, i save the new user, he still has the old properties after loading again
Kev
@lineus
Mar 06 2018 14:18
yeah, I'm struggling with it too. gist
julian libor
@Linux249
Mar 06 2018 14:23
i wrote me a method now thats do the following on every doc in the db `
```
         const oldUser = user.toObject()
        await user.remove()
        const newUser = new User(oldUser)
        const saved = await newUser.save()
Kev
@lineus
Mar 06 2018 14:52
@Pritilender when you have a moment, can you take a look at my gist and see if I'm doing something wrong? I based it off of the docs here
Mihajlo Ilijić
@Pritilender
Mar 06 2018 14:52
@lineus I'll take a look in 5-10 mins, just to finish something
Kev
@lineus
Mar 06 2018 14:52
no rush at all, thanks!
Mihajlo Ilijić
@Pritilender
Mar 06 2018 15:13
Hey @lineus everything looks good
You can even see on this line that strictQuery is working https://gist.github.com/lineus/e068372b825dd0a4ebfd3e90a2b842bb#file-2_find-js-L23
Kev
@lineus
Mar 06 2018 15:15
so my expectation that the someValue field would be stripped is incorrect?
Mihajlo Ilijić
@Pritilender
Mar 06 2018 15:15
No, they are correct
Look at the Mongo query
it's .find({})
it's not .find({someValue: 1})
it has someValue stripped off because you're using strictQuery: true
remove it, and you'll see that generated query looks like .find({someValue: 1})
Ok, I now see your comment (in a comment :D that's why I haven't noticed it at first).
Kev
@lineus
Mar 06 2018 15:17
so the field gets stripped from the query, as opposed to the field being stripped from the document that is returned. gotcha.
I kinda expected my expectations were to blame, as opposed to a bug.
Mihajlo Ilijić
@Pritilender
Mar 06 2018 15:17
I think you've mentioned it earlier, but just as a reminder, Mongoose will return whatever there is in the collection on which schema is defined.
It's the nature of Mongo as NoSQL, document DB.
I'm not sure can you do returnedDoc.someValue if your schema doesn't have someValue defined.
Kev
@lineus
Mar 06 2018 15:19
It makes sense, when I read the docs for strictQuery, I thought mongoose was doing some magic with the docs returned from the db, but it's just casting (in this case deleting) the query.
I learned something today! thanks @Pritilender
Mihajlo Ilijić
@Pritilender
Mar 06 2018 15:21
If you have time, try to get .someValue from doc returned. I'm interested will Mongoose complain about it or not. But my guess is that it won't...
Which can be a bit confusing.
I've found this issue Automattic/mongoose#3404
Mihajlo Ilijić
@Pritilender
Mar 06 2018 15:26
Basically, Mongoose will not strip off fields unless you specify it with select.
I also learned something today @lineus :)
Kev
@lineus
Mar 06 2018 15:30
InspiredMacPro:Linux249 lineus$ ./find.js 
Mongoose: tests.find({}, { fields: {} })
res[0]: { _id: 5a9eb37875addf67ed889664,
  name: 'Geo Kub',
  someValue: [ { value: 'SDD', _id: 5a9eb37875addf67ed889663 } ],
  __v: 0 }
res[0].someValue: undefined
^C
InspiredMacPro:Linux249 lineus$
that is confusing to me. here is how I printed that out:
  console.log('res[0]:', res[0])
  console.log('res[0].someValue:', res[0].someValue)
printing res[0] shows someValue, but printing res[0].someValue is undefined
Mihajlo Ilijić
@Pritilender
Mar 06 2018 15:51
Well... Probably document.toString() prints the whole document.
And document.someValue is undefined getter.
Kev
@lineus
Mar 06 2018 15:58
that makes sense. thanks, I feel better lol.
Mihajlo Ilijić
@Pritilender
Mar 06 2018 16:13
Nice :D
Paweł Otto
@pawelotto
Mar 06 2018 18:52
Yes there is
Kev
@lineus
Mar 06 2018 20:19
Maybe there is, maybe there isn't :)