getting schema attributes from Mongoose Model

MongodbMongoose

Mongodb Problem Overview


I'm using Mongoose.js to create models with schemas.

I have a list of models (many) and at times I'd like to get the attributes/keys that make up a particular model.

Is there a method to pull out the attribute schemas for any given model?

For example,

var mySchema = module.exports = new Schema({
  SID: {
    type: Schema.Types.ObjectId
    //, required: true
  },
  teams: {
    type: [String]
  },
  hats: [{
    val: String,
    dt: Date
  }],
  shields: [{
    val: String,
    dt: Date
  }],
  shoes: [{
    val: String,
    dt: Date
  }]
}

);

Is it possible to pull out/identify the attributes of the schema [SID, hats, teams, shields, shoes]??

Mongodb Solutions


Solution 1 - Mongodb

Yes, it is possible.

Each schema has a paths property, that looks somewhat like this (this is an example of my code):

paths: {
    number: [Object],
    'name.first': [Object],
    'name.last': [Object],
    ssn: [Object],
    birthday: [Object],
    'job.company': [Object],
    'job.position': [Object],
    'address.city': [Object],
    'address.state': [Object],
    'address.country': [Object],
    'address.street': [Object],
    'address.number': [Object],
    'address.zip': [Object],
    email: [Object],
    phones: [Object],
    tags: [Object],
    createdBy: [Object],
    createdAt: [Object],
    updatedBy: [Object],
    updatedAt: [Object],
    meta: [Object],
    _id: [Object],
    __v: [Object]
}

You can access this through an model too. It's under Model.schema.paths.

Solution 2 - Mongodb

Don't have enough rep to comment, but this also spits out a list and loops through all of the schema types.

mySchema.schema.eachPath(function(path) {
    console.log(path);
});

should print out:

number
name.first
name.last
ssn
birthday
job.company
job.position
address.city
address.state
address.country
address.street
address.number
address.zip
email
phones
tags
createdBy
createdAt
updatedBy
updatedAt
meta
_id
__v

Or you could get all Attributes as an Array like this:

var props = Object.keys(mySchema.schema.paths);

Solution 3 - Mongodb

My solution uses mongoose model.

Schema attributes

const schema = {
  email: {
    type: String,
    required: 'email is required',
  },
  password: {
    type: String,
    required: 'password is required',
  },
};

Schema

const FooSchema = new Schema(schema);

Model

const FooModel = model('Foo', FooSchema);

Get attributes from model:

Object.keys(FooModel.schema.tree)

Result:

[  'email',  'password',  '_id',  '__v'] 

Solution 4 - Mongodb

Solution for lodash, function which picked all schema properties, excluding specified

_.mixin({ pickSchema: function (model, excluded) {
    var fields = [];
    model.schema.eachPath(function (path) {
       _.isArray(excluded) ? excluded.indexOf(path) < 0 ? fields.push(path) : false : path === excluded ? false : fields.push(path);
    });
    return fields;
   }
});

_.pickSchema(User, '_id'); // will return all fields except _id

_.pick(req.body, _.pickSchema(User, ['_id', 'createdAt', 'hidden'])) // all except specified properties

read more here https://gist.github.com/styopdev/95f3fed98ce3ebaedf5c

Solution 5 - Mongodb

You can use Schema.prototype.obj that returns the original object passed to the schema constructor. and you can use it in a utility function to build the object you're going to save.

import Todo from './todoModel'
import { validationResult } from 'express-validator'

const buildObject = (body) => {
    const data = {};
    const keys = Object.keys(Todo.schema.obj);
    keys.forEach(key => { if (body.hasOwnProperty(key)) data[key] = body[key] })
    return data;
}

const create = async (req, res) => {
    try {
        const errors = validationResult(req);
        if (!errors.isEmpty()) return res.json(errors);
        let toBeSaved = buildObject(req.body);
        const todo = new Todo(toBeSaved);
        const savedTodo = await todo.save();
        if (savedTodo) return res.json(savedTodo);
        return res.json({ 'sanitized': keys })
    } catch (error) {
        res.json({ error })
    }
}

another way is to to not call the buildObject function and add it in two lines but you will write every key you want to save

let { title, description } = req.body;
let toBeSaved = { title, description };

Using ES6 shorthand property names

Solution 6 - Mongodb

If you want to have only the attributes you added and not the add methods by the ORM that starts with '$___', you have to turn the document into object then access the attributes like this:

Object.keys(document.toObject());

Solution 7 - Mongodb

The accepted answer did not work for me. But using Mongoose 5.4.2 I was able to get the keys by doing the following:

const mySchema = new Schema({ ... });

const arrayOfKeys = Object.keys(mySchema.obj);

I'm using typescript, however. That might have been the problem.

Solution 8 - Mongodb

In case you want to have all property values (including nested and populated properties), just use toObject() method:

let modelAttributes = null;
SomeModel.findById('someId').populate('child.name').exec().then((result) => {
  modelAttributes = result.toObject();
  console.log(modelAttributes);
});

The output would be:

{
  id: 'someId',
  name: 'someName',
  child: {
    name: 'someChildName'
  }
  ...
}

Solution 9 - Mongodb

Just insert the field name you like to get.

let fieldName = 'birthday'
console.log(mySchema.schema.paths[fieldName].instance);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1460015View Question on Stackoverflow
Solution 1 - MongodbgustavohenkeView Answer on Stackoverflow
Solution 2 - Mongodbjs_gandalfView Answer on Stackoverflow
Solution 3 - Mongodblucas telesView Answer on Stackoverflow
Solution 4 - MongodbstyopdevView Answer on Stackoverflow
Solution 5 - MongodbMohammed RamadanView Answer on Stackoverflow
Solution 6 - Mongodbe-nouriView Answer on Stackoverflow
Solution 7 - MongodbcrownlesskingView Answer on Stackoverflow
Solution 8 - MongodbFootnikoView Answer on Stackoverflow
Solution 9 - MongodbDilshan LiyanageView Answer on Stackoverflow