Previously I didn't declare the _id in my Schema, so each new submission naturally will have MongoDB generated ObjectId as it's _id. However, the requirements have changed and now _id is declared as String as below.
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var MySchema = new Schema({
_id: {
type: String,
},
schoolID: {
type: mongoose.Schema.Types.ObjectId, ref: 'School'
},
points: {
type: Number
},
});
MySchema.index({ schoolID : 1})
module.exports = mongoose.model('Submission', MySchema);
However, now I cannot find my previously inserted documents using _id at all. I tried
var submissionId = "60654319a8062f684ac8fde4"
Submission.findOne({ _id: mongoose.mongo.ObjectId(submissionId ) })
Submission.findOne({ _id: mongoose.Types.ObjectId(submissionId ) })
Submission.findOne({ _id: mongoose.ObjectId(submissionId ) })
but it will always return null. So when I use var mongoose = require('mongoose').set('debug', true); to check, it will display below; that all my queries above will still find using String, and not ObjectId
Mongoose: submission.findOne({ _id: '60654319a8062f684ac8fde4' }, { projection: {} })