Mongoose String to ObjectID
<p>i have string with ObjectId .</p>

<pre><code>var comments = new Schema({
    user_id:  { type: Schema.Types.ObjectId, ref: 'users',required: [true,'No user id found']},
    post: { type: Schema.Types.ObjectId, ref: 'posts',required: [true,'No post id found']}....

export let commentsModel: mongoose.Model&lt;any&gt; = mongoose.model("comments", comments);
</code></pre>

<p><em>How i user it:</em> </p>

<pre><code>let comment = new commentsModel;
str = 'Here my ObjectId code' //
comment.user_id = str;
comment.post = str;
comment.save();
</code></pre>

<p>When I create a "comment" model and assign a string user_id value or post I have an error when saving. I make <code>console.log(comment)</code> all data is assigned to vars.</p>

<p>I try:</p>

<pre><code> var str = '578df3efb618f5141202a196';
    mongoose.mongo.BSONPure.ObjectID.fromHexString(str);//1
    mongoose.mongo.Schema.ObjectId(str);//2
    mongoose.Types.ObjectId(str);//3
</code></pre>

<ol>
<li>TypeError: Object function ObjectID(id) {</li>
<li>TypeError: Cannot call method 'ObjectId' of undefined</li>
<li>TypeError: Cannot read property 'ObjectId' of undefined</li>
</ol>

<p>And of course I included the mongoose <strong>BEFORE ALL CALLS</strong></p>

<pre><code>import * as mongoose from 'mongoose';
</code></pre>

<p>nothing works.</p>