Reputation: 12712
anyone know what the simplest way to check whether an object is a mongoose object? Am I just best checking if toObject() is defined or is there a more efficient way. many thanks
Upvotes: 33
Views: 21638
Reputation: 516
This, if you want to make sure it's the model you expect.
dog.constructor.modelName === 'Dog'
Upvotes: 1
Reputation: 2932
If you are working with typescript and using typescript imports then this helps
import { Mongoose } from 'mongoose';
...
const isMongooseModel = (object instanceof Mongoose.prototype.Model); // true if object is of type Model
const isMongooseDocument = (object instanceof Mongoose.prototype.Document); // true if object is of type Document
const isMongooseQuery = (object instanceof Mongoose.prototype.Query); // true if object is of type Query
Upvotes: 0
Reputation: 480
One way to determine is if you do a
if (data.toObject)
it will return a Function statement if its true, will return undefined if it isn't;
Upvotes: 3
Reputation: 562
Try this:
var mongoose = require('mongoose');
function isMongoModel(yourObject){
return yourObject.hasOwnProperty('schema') && yourObject.schema instanceof mongoose.Schema;
}
Upvotes: 1
Reputation: 119
My preferred way to determine if an object is a Mongoose model is almost as simple as the above, but not quite:
function isModel(obj) {
obj = obj || {}
return obj.prototype instanceof mongoose.Model
}
In the context where I use this, I don't want to get undefined back, but do need to verify that I haven't gotten a "3" or some such oddball value where I expect a model.
Of course, after thinking further about this, there's more than one way to be a "Mongoose object." I've sometimes mistaken a DTO for a Mongoose model and then misused it later, hence my test above...but if I just needed to verify that something is an instance, but for some reason could not know the instance type, I would do something like what Lukasz did above and look at the constructor base.
Upvotes: 11
Reputation: 5858
Another simple way:
const isMongooseModel = (object instanceof Mongoose.Model);
Upvotes: 4
Reputation: 15432
To check whether obj
is a Mongoose object, use this snippet:
const _ = require('lodash');
const mongoose = require('mongoose');
function checkIfMongooseObject(obj) {
return _.get(obj, 'constructor.base') instanceof mongoose.Mongoose;
}
Contrary to other solutions provided, this one is safe - it will never fail regardless type of obj
(be it even String or Int).
Upvotes: 2
Reputation: 7835
The follwing for me in the case of checking when an ObjectID is a populated object or just an ObjectID:
if (object._id.constructor.name === 'ObjectID') {
// Not a populated object, only its ID
}
Upvotes: 2
Reputation: 311935
You can check the object's prototype via the instanceof
operator to confirm it's an instance of your mongoose model. Using the example schema from mongoosejs.com:
if (obj instanceof Cat) {
// yes, it's a mongoose Cat model object
...
}
Upvotes: 37
Reputation: 4337
I'm using this
if (object.constructor.name === 'model') {
// object is mongoose object
}
Upvotes: 22