1
3 Comments

How to do this?

Using mongodb or preferably, I want to create a web app that allows a user to create an object, the object’s fields will be dynamic. So I can’t use the mongoose to pre-set the scheme before saving to db.

Ex.

Customer A.

Object: { name: John doe, age: forty five }

Customer B.

Object: { sex: male, height: six }

All the values will be string but the customer gets to decide the field names.

I would like to save these objects as is given.

Is using the strict false in mongoose the best method for this?

on June 24, 2020
  1. 1

    Skip ORM like mongoose and interact with Mongo directly. ORM are used when you want to impose a schema restriction on your mongo collection. The solution you are looking for is to use the native driver and put the data in directly to your collection. Dynamic Schema is one of the major benefits of no-sql databases.
    If you still want to stick with mongoose, have a look at something called mixed schema.

    1. 1

      I’d like to stay with mongoose but I’ve read some places about mongo queries being faster, do you know anything about that?

      1. 1

        Mongoose is just an abstraction layer over mongo which controls how the queries are formed and keeps you away from the real juicy pieces. Using native driver, you can write more performing query and will be able to structure them in the way your indexes are defined. Have not used mongoose in a long time, i migrated to native mongo long ego. if you would like to give a read, have a look at this article which has some benchmark numbers for mongo and mongoose.

        https://medium.com/@bugwheels94/performance-difference-in-mongoose-vs-mongodb-60be831c69ad

        Also, with all the new advances in mongo world, it has lot of things under it than what mongoose offer.

  2. 1

    This comment was deleted 6 years ago