mhendek
mhendek

Reputation: 273

MongoDb - To create a new ObjectId please try `Mongoose.Types.ObjectId` instead of using `Mongoose.Schema.ObjectId`

I am new to Node.js and mongodb.

In my Node.js application, I am using mongodb and for mongodb operations I am using mongoose.

In the package.json file, I have the following dependency:

"mongoose": "4.11.9"

Although my application works (I can do everything that I want), In the server log I always see the following error message:

To create a new ObjectId please try Mongoose.Types.ObjectId instead of using Mongoose.Schema.ObjectId.

How can I solve this problem ?

Upvotes: 2

Views: 5085

Answers (2)

user9111756
user9111756

Reputation:

What you are seeing is a warning message ( as opposed to an error which would cause your node process to exit ).

consider this example:

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

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
const conn = mongoose.connection;
const Schema = mongoose.Schema;

const schema = new Schema({
  myRef: {
    type: Schema.Types.ObjectId,
    ref: 'someothercollection'
  }
});

const Test = mongoose.model('test', schema);

const test = new Test({
  myRef: Schema.ObjectId() // or Schema.Types.ObjectId()
});

const test2 = new Test({
  myRef: mongoose.Types.ObjectId()
});

async function run() {
  await conn.dropDatabase();
  let doc = await test.save();
  console.log(doc);
  let doc2 = await test2.save();
  console.log(doc2);
  return conn.close();
}

run();

outputs:

stack: ./49841247.js
mongoose: To create a new ObjectId please try `Mongoose.Types.ObjectId` instead of using `Mongoose.Schema.ObjectId`. Set the `suppressWarning` option if you're trying to create a hex char path in your schema.
Trace
    at Function.ObjectId (/Users/lineus/dev/Help/mongoose5/node_modules/mongoose/lib/schema/objectid.js:30:13)
    at Object.<anonymous> (/Users/lineus/dev/Help/mongoose5/stack/49841247.js:19:17)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Function.Module.runMain (module.js:693:10)
    at startup (bootstrap_node.js:188:16)
    at bootstrap_node.js:609:3
{ _id: 5ad33e173a19606ffa47c95c, __v: 0 }
{ _id: 5ad33e173a19606ffa47c95e,
  myRef: 5ad33e173a19606ffa47c95d,
  __v: 0 }
stack:

You can see that:

  1. the myRef property of the first document doesn't get set at all and it prints the warning message.
  2. In the second document, the myRef property is set properly.

If you are getting this warning, then most likely, somewhere in your code you are using the wrong method to generate an objectId.

If you aren't manually generating an objectId incorrectly, check out the next answer at the time of this edit.

Upvotes: 1

Zeeshan Hassan Memon
Zeeshan Hassan Memon

Reputation: 8325

It has been fixed for mongoose 4.11.10 and later versions, so try upgrading your version to get rid of the warning.

This was an unintentional side effect of #5571 because employees.certifications happens to be exactly 24 characters long.

var mongoose = require('mongoose');

var zoneSchema = new mongoose.Schema({
    employees: {
        certifications: {type: mongoose.Schema.Types.ObjectId, ref: 'Certifications'}
    }
});

var certificationsSchema = new mongoose.Schema({
    type: String,
    list: [{type: mongoose.Schema.Types.ObjectId, ref: 'Certification'}]
});

var certificationSchema = new mongoose.Schema({
    name: String
});

var Zone = mongoose.model('Zone', zoneSchema);
var Certifications = mongoose.model('Certifications', certificationsSchema);
var Certification = mongoose.model('Certification', certificationSchema);

var newZone = new Zone();

For further reading have a look at issue#5587 & issue#5571

Upvotes: 4

Related Questions