Mongoose: check if object is mongoose object

最后都变了- 提交于 2020-12-02 02:16:39

问题


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


回答1:


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
    ...
}



回答2:


I'm using this

if (object.constructor.name === 'model') {
  // object is mongoose object
}



回答3:


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.




回答4:


Another simple way:

const isMongooseModel = (object instanceof Mongoose.Model);



回答5:


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;




回答6:


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
} 



回答7:


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).




回答8:


Try this:

var mongoose = require('mongoose');

function isMongoModel(yourObject){

    return yourObject.hasOwnProperty('schema') && yourObject.schema instanceof mongoose.Schema;
}



回答9:


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




回答10:


This, if you want to make sure it's the model you expect.

dog.constructor.modelName === 'Dog'



来源:https://stackoverflow.com/questions/10827108/mongoose-check-if-object-is-mongoose-object

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!