Reputation: 2195
So I have a mongoose schema which has a following field:
var orderSchema = new Mongoose.Schema({
status:{
type: Types.String,
enum: ['0','2', '6', '9', '10', '11', '12'],
default: '0'
}
});
Mongoose.model("order", orderSchema);
The above status field is of type string.
Assume you have a status field with the value "2"
in the db.
If I run the below query from mongo shell, it will not give any result:
db.orders.findOne({status: 2});
will not work and as expected, this will work
db.orders.findOne({status: "2"});
However, when I executed the query using mongoose(ver:"^4.13.14") in my node.js code This works:
orders.findOne({status: 2})
So, wanted to understand if mongoose does this conversion internally based on the defined schema? Or something else is happening under the hood.
Upvotes: 2
Views: 545
Reputation: 49945
Enum in mongoose is just a string
with additional validator. You can observe the same behavior for regular strings like:
let orderSchema = new mongoose.Schema({
status:{
type: String
}
});
let Order = mongoose.model("orders", orderSchema);
let doc = await Order.findOne({ status: 2 });
Browsing the find
docs you can read that:
The conditions are cast to their respective SchemaTypes before the command is sent.
Upvotes: 2