These are chat archives for Automattic/mongoose

17th
Mar 2018
Gustav Larsson
@gustavlrsn
Mar 17 2018 08:30
Does Mongoose convert ObjectID's to strings automatically when querying? :)
Juha Lindstedt
@pakastin
Mar 17 2018 08:41
If you query with ObjectID, then yeah, but results are ObjectID’s, which is sometimes confusing..
..because you can’t compare with === you have to use id.equals(anotherId)
Or id.toString() === anotherId
Gustav Larsson
@gustavlrsn
Mar 17 2018 08:44
@pakastin Ok, thanks! I guess I was a bit confused because they were automatically converted to strings when consumed as JSON objects, but not prior to that
Juha Lindstedt
@pakastin
Mar 17 2018 08:47
Yeah, JSON can only contain JSON objects, arrays, strings, numbers, booleans and null.
Kev
@lineus
Mar 17 2018 10:20
yeah, that confused me at first too @gustavlrsn. There's a document method called toObject() that gets called ( source is in node_modules/mongoose/lib/documents.js). There are options you can add to change the behavior of toObject() docs here
Kev
@lineus
Mar 17 2018 10:27
to be completely accurate, there are two methods toJSON() and toObject(), that both call the same private method $toObject().
Gustav Larsson
@gustavlrsn
Mar 17 2018 10:44
Cool! thanks @linues
If my model specifies that a certain field is an ObjectId, then mongoose converts a string supplied to that field to an ObjectId when I save something to a collection. This at least is my observed behavior, which is convenient, is there anything wrong with doing it like that?
Kev
@lineus
Mar 17 2018 10:50
I don't think there's anything wrong with that. In my experience mongoose casts values the way it says it will. I'm pretty new at this though, so others with more experience might have a different take.
are you generating your own objectIds @gustavlrsn ?
or are you referring to references to other docs?
Gustav Larsson
@gustavlrsn
Mar 17 2018 10:56
i'm letting mongoose generate objectIds
but for example when creating a Comment I supply a objectId (as a string) that references a Post
so yes, i'm referring to references :)
And I'm also doing something like this:
export const Comment = mongoose.model('Comment', {
  content: String,
  authorId: mongoose.Schema.Types.ObjectId,
  postId: mongoose.Schema.Types.ObjectId,
  created_at: {
    type: Date,
    default: Date.now,
  },
});
i.e. not using the ref field that mongoose specifies
because I'm using mongoose beneath a graphQL schema that takes care of the relationships and what not, so I figured I don't need to do joins with mongoose itself
Kev
@lineus
Mar 17 2018 11:01
trying out graphql is on my list of things to do. It's right next to typescript and about 50 other things. I'm still getting the basics of javascript down though :)
Gustav Larsson
@gustavlrsn
Mar 17 2018 11:04
starting with the basics is not a bad idea :)
Gustav Larsson
@gustavlrsn
Mar 17 2018 11:17
but with that said, GraphQL is really cool and fairly straightforward, I encourage you to try it out!
Ghost
@ghost~5928d90bd73408ce4f629b9e
Mar 17 2018 11:52
why does findbyid doesn't return the object details
Kev
@lineus
Mar 17 2018 11:53
what does your call to findById look like @2HexGFX_twitter ?
oh, heh, I see you posted it to node just now :)
Ghost
@ghost~5928d90bd73408ce4f629b9e
Mar 17 2018 11:54
        db.collection('users').findById({
            _id: require("mongodb").ObjectId(req.session.id)
        }, (err, succ) => {
            if(err) {
                console.log(err)
                reject(err)
                res.end()
            }

            if(succ){
                console.log(succ)
                resolve(succ)
                res.end()
            }
        })
is this
do you know why @lineus
Mihajlo Ilijić
@Pritilender
Mar 17 2018 11:55
@2HexGFX_twitter findById first param is not an object, it's just id
Kev
@lineus
Mar 17 2018 11:56
he's calling the findById on the native driver there isn't he @Pritilender ?
Mihajlo Ilijić
@Pritilender
Mar 17 2018 11:56
so you would need db.collection('users').findById(req.session.id)
@lineus yes
Kev
@lineus
Mar 17 2018 11:56
does that take a string too? never seen that one before :)
Mihajlo Ilijić
@Pritilender
Mar 17 2018 11:56
Nope, it doesn't exist, as far as I see it in the docs.
My bad...
Anyhow, db.collection('users').findOne({_id: ObjectId(req.session.id)} should do fine
Also @2HexGFX_twitter for the love of God, please put all your requires at the start of your .js file.
Ghost
@ghost~5928d90bd73408ce4f629b9e
Mar 17 2018 11:58
ur right i was also using node driver i think findbyid doesnt exist here
Mihajlo Ilijić
@Pritilender
Mar 17 2018 11:58
const ObjectId = require('mongodb').ObjectId
Ghost
@ghost~5928d90bd73408ce4f629b9e
Mar 17 2018 11:58
findOne did the job i actually tried this earlier same result but now its working maybe because of objectid instead of just req.session.id
Mihajlo Ilijić
@Pritilender
Mar 17 2018 11:59
Yes, mongo driver requires for types to match. It won't cast string to ObjectId.
Kev
@lineus
Mar 17 2018 18:53
I’m not sure you can call process.exit() in a way that won’t immediately exit completely. Maybe throwing an error makes more sense instead and then expect() it to throw?
@ankur-anand ^
Man, Gitter is less than ideal on mobile.
Ankur Anand
@ankur-anand
Mar 17 2018 18:59
@lineus thank's for comment :) .. Sorry i've posted the message in the wrong group without noticing so deleted ..