简体   繁体   中英

Object metadata (schema) design in mongodb

First of all excuse me since I don't know how it is called in computer since:

For each of my document types in my mongo app I want to define a structure, with every field defined with its constraints, validation patterns and, generally, roles that can view modify and delete this document.

For example: Book :

{
 name: "Book",
 viewRoles: ["Admin","User"],
 createRoles: ["Admin"],
 modifyRoles: ["Admin", "User"],
 fields: [
 {
    id:"title",
    name:"Book Title",
    validation: "",
    maxLength: 50,
    minLength: 3,
    required: true
   },
   {
    id:"authorEmail",
    name:"Email of the Author",
    validation: "email",
    maxLength: 50,
    minLength: 3,
    required: false
   }
 ]
}

Then if I have this "schema" for all of my documents, I can have one view for creating modifying and showing this "entities".

I also want to have the ability to create new document types, modify their fields through admin panel of my application.

When I google "mongo dynamic schema", "mongo document meta design" I get useless information.

My question is how it is called -- when I want to have predefined schema of my documents and have the ability to modify it. Where I can get more information about how to design such systems?

Since you tagged this as having a Meteor connection, I'll point you to Simple Schema: https://github.com/aldeed/meteor-simple-schema/ . I use it, along with the related collection2 package. I find it's a nice way to document and enforce schema design. When used with the autoform package, it also provides a way to create validated forms directly from your schema.

I think you are looking for how to model your data. The below link might be helpful:

http://docs.mongodb.org/manual/data-modeling/

I also want to have the ability to create new document types, modify their fields through admin panel of my application.

For Administrative activities you may look into the options given in:

http://docs.mongodb.org/ecosystem/tools/administration-interfaces/

And once you are done, you might want to read this as a kick off:

https://blog.serverdensity.com/mongodb-schema-design-pitfalls/

In Mongo DB you don't create collections. You just start using them. So you can't define schemas before hand. The collection is created on the first insert you make to the collection. Just make sure to ensure Index on the collection before inserting documents into it:

db.collection.ensureIndex({keyField: 1})

So it all depends on maintaining the structure of the documents inserted to the collection rather than defining the collection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM