These are chat archives for Automattic/mongoose

17th
Apr 2018
Arun Gadag
@arun-awnics
Apr 17 2018 11:43
Is somebody there?
@lineus Kev?
Arun Gadag
@arun-awnics
Apr 17 2018 11:53

This is my message model:

var MessageSchema = new Schema({
    _id: {
        type: Schema.ObjectId,
        auto: true
    },
    text: String,
}, { collection: 'message' });

And this is how I am creating a new message and store it in db:

var message= new Message(msg);
message.save((err, message) => {
        if (err) {
            log.error('err is: ', JSON.stringify(err));
        }
        console.log(message._id); // _id is always null
    });
How can I get the value of _id
?
fredski02
@fredski02
Apr 17 2018 11:54
mongodb will auto populate an _id for you
does the message actually get created?
Kev
@lineus
Apr 17 2018 11:56
it should be Schema.Types.ObjectId, you'll get a warning and no value stored with just Schema.ObjectId
@arun-awnics ^
Arun Gadag
@arun-awnics
Apr 17 2018 11:57
I tried both Schema.Types.ObjectId and Schema.ObjectId
None work
fredski02
@fredski02
Apr 17 2018 11:57
you dont need it
Arun Gadag
@arun-awnics
Apr 17 2018 11:57
I can see the _id created
Kev
@lineus
Apr 17 2018 11:57
yeah, theres that too :)
Arun Gadag
@arun-awnics
Apr 17 2018 11:57
I tried without declaring _id too :D
fredski02
@fredski02
Apr 17 2018 11:57
console.log(message)
tell us what it shows
or send us the output rather
Arun Gadag
@arun-awnics
Apr 17 2018 11:58
Wait.. I will send you the screenshot
{"__v":0,"_id":null,text":"jsdjdfjksdf"}
This is the object that is returned if I log it
@fredski02 But when I check in db, i.e., db.message.find() shows the _id
@lineus
Kev
@lineus
Apr 17 2018 12:03
is the _id that find shows a BSON ObjectId? or a string?
fredski02
@fredski02
Apr 17 2018 12:04
did you remove the entire field from your schema initialisation?
so it looks like this instead
var MessageSchema = new Schema({
    text: String
}, { collection: 'message' });
you dont need the _id field
mongodb auto populates it for you
Arun Gadag
@arun-awnics
Apr 17 2018 12:06
@fredski02 Yeah. I removed the _id field and checked
Its still null
I didn't have _id filed in the first place. I know mongo auto populates it
But I read a few SO answers in which they had told to add it and mention the type
Hence I tried that but with no success
@lineus It returns the JSON object with _id
niluroy
@niluroy
Apr 17 2018 12:08
image.png
Arun Gadag
@arun-awnics
Apr 17 2018 12:09
So... That's the output for db.message.find()
@lineus @fredski02
fredski02
@fredski02
Apr 17 2018 12:10
okay
you're using mongoose?
Arun Gadag
@arun-awnics
Apr 17 2018 12:10
Yes
fredski02
@fredski02
Apr 17 2018 12:11
try using their create method. perhaps there are peculiarities with the new Object method - http://mongoosejs.com/docs/api.html#create_create
Arun Gadag
@arun-awnics
Apr 17 2018 12:11
"mongoose": "^4.13.11",
fredski02
@fredski02
Apr 17 2018 12:12
var promise = Candy.create({ type: 'jawbreaker' });
promise.then(function (jawbreaker) {
  // ...
})
Arun Gadag
@arun-awnics
Apr 17 2018 12:13
try using their create method. perhaps there are peculiarities with the new Object method - http://mongoosejs.com/docs/api.html#create_create
But I am using version 4
var promise = Candy.create({ type: 'jawbreaker' });
promise.then(function (jawbreaker) {
  // ...
})
You think its because of this?
Mongoose already returns a promise right?
Kev
@lineus
Apr 17 2018 12:15
@arun-awnics what is the value that you are passing in as msg to new Message(msg)
if you attempt to create a new Message by passing in an object with { _id: null, text: 'some text' } mongoose will not auto add the _id value for you.
fredski02
@fredski02
Apr 17 2018 12:21
use a callback then
see callback method instead.
Candy.create({ type: 'jelly bean' }, { type: 'snickers' }, function (err, jellybean, snickers) {
  if (err) // ...
});
Kev
@lineus
Apr 17 2018 12:22
@arun-awnics here's a gist that shows the difference.
Arun Gadag
@arun-awnics
Apr 17 2018 12:29
You are right @lineus
I had an ._id field in Message object
And I was passing null value to it assuming that mongo will update the new value
@arun-awnics here's a gist that shows the difference.
This worked :thumbsup:
Thanks a lot
Kev
@lineus
Apr 17 2018 12:31

the funky part is that when you saved the doc to mongodb, the server overwrote the null value in _id with an ObjectId:

gitter: mongo --quiet test
> db.tests.find()
{ "_id" : ObjectId("5ad5e8258cd4d4ccbad4ec02"), "text" : "Broken", "__v" : 0 }
{ "_id" : ObjectId("5ad5e8258cd4d4ccbad4ec01"), "text" : "Working", "__v" : 0 }
>

but ( of course ) mongoose was totally unaware of it :) until you find it by text

you're welcome @arun-awnics!
Arun Gadag
@arun-awnics
Apr 17 2018 12:34

the funky part is that when you saved the doc to mongodb, the server overwrote the null value in _id with an ObjectId:

gitter: mongo --quiet test
db.tests.find()
{ "_id" : ObjectId("5ad5e8258cd4d4ccbad4ec02"), "text" : "Broken", "__v" : 0 }
{ "_id" : ObjectId("5ad5e8258cd4d4ccbad4ec01"), "text" : "Working", "__v" : 0 }

but ( of course ) mongoose was totally unaware of it :) until you find it by text

:D
This is weird, isn't this?
If value of _id is being updated as I save the document, why isn't the updated value being returned?
Typically, it should return the updated value, isn't it?
Kev
@lineus
Apr 17 2018 12:36
the mongoose docs say that you have to have an _id field on top level docs, but it can be anything really, if you made it a string, mongodb wouldn't change it for instance.
Arun Gadag
@arun-awnics
Apr 17 2018 12:38
Yeah. I get it
But I am not sending a string.
I am not sending any value
Kev
@lineus
Apr 17 2018 12:39
It would be helpful if mongoose were to throw an error with _id: null
Arun Gadag
@arun-awnics
Apr 17 2018 12:39
Hence I was confused why it wasn't returning the value
Yeah. Probably they should be handling such issues before executed.
Do you think its an issue in the framework?
Kev
@lineus
Apr 17 2018 12:42
:thought_balloon:
Kev
@lineus
Apr 17 2018 12:49
passing in _id: null causes this behavior even when you don't explicitly define _id in schema, so I would say it's undesirable and worth an issue in the mongoose queue.
Kev
@lineus
Apr 17 2018 12:55
as a feature request for both versions 4 and 5. And IMO, the request should be for an error to be thrown as opposed to inserting the doc and retrieving the mongodb generated _id. It looks like mongodb uses existing objectId's from the current collection to determine what it stores for the _id. This behavior could clash with _ids generated by mongoose.
Arun Gadag
@arun-awnics
Apr 17 2018 13:00
Yeah. This is not even part of the documentation. As you said, it would be easy if an error is thrown. It would have been difficult for me alone to find out this issue today if it was not fixed by you.
If users want to create an id explicitly, they would create a property themselves and add the same there.
_id here behaves like a primary key and it should retain the same behaviors
Kev
@lineus
Apr 17 2018 13:09
it's mentioned below this under option: _id that you have to have an _id field on top level documents and that you should get an error if you try to save a doc without an _id. I'll take a look at the source and see why _id: null is slipping through.
my guess is it's just looking for the existence of the property, or that it isn't === undefined.
Mihajlo Ilijić
@Pritilender
Apr 17 2018 19:49
Hey, does anyone know how can I get lean object from my model?
Nicholas Krause
@xerofoify
Apr 17 2018 19:56

I am getting a rather weird error in my logs on heroku: TypeError: User is not a constructor. Here is my code and does anyone know why it doesn't work on line 50 which is this let newUser = new User(userData); https://pastebin.com/5jL0QFP4

.

Kev
@lineus
Apr 17 2018 23:38
@Pritilender, you mean from a query like findOne?
Mihajlo Ilijić
@Pritilender
Apr 17 2018 23:39
@lineus there's a model from find query and I need it to be a model until one moment, and at that moment I want to strip it off all model properties and have what would I get if I would call find().lean()
I tried .toJson() and the model doesn't have it
Kev
@lineus
Apr 17 2018 23:40
It should have toJSON() or toObject()
Mihajlo Ilijić
@Pritilender
Apr 17 2018 23:40
toObject!
right!
I knew there was something with to
I'll give it a go tomorrow, too tired right now :D
thanks!
Kev
@lineus
Apr 17 2018 23:42
Anytime :)
Mihajlo Ilijić
@Pritilender
Apr 17 2018 23:42
the old team we've inherited the project from was combining a placeholder-like user model until they determine if there's a user already present in the db, but then, if it's in the db, they are calling merge from lodash and because what is returned from the db is a model and that placholder is also a model, then new id is given to the old model, even though merge is called on sub-document
and I was looking at the code and questioning how can user._id be one thing and user.id be another...
so after an hour or two looking at it, it clicked :D
sorry for the rant, I really hate what those guys did and kinda understand why people have bad opinion towards JS
Kev
@lineus
Apr 17 2018 23:45
Don’t be sorry, I feel your pain.
Mihajlo Ilijić
@Pritilender
Apr 17 2018 23:48
Thanks :D