The two types of objects seem to be so close to one another that having both feels redundant. What is the point of having both schemas and models?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
EDIT: Although this has been useful for many people, as mentioned in the comments it answers the "how" rather than the why. Thankfully, the why of the question has been answered elsewhere also, with this answer to another question. This has been linked in the comments for some time but I realise that many may not get that far when reading.
Often the easiest way to answer this type of question is with an example. In this case, someone has already done it for me 🙂
Take a look here:
http://rawberg.com/blog/nodejs/mongoose-orm-nested-models/
EDIT: The original post (as mentioned in the comments) seems to no longer exist, so I am reproducing it below. Should it ever return, or if it has just moved, please let me know.
It gives a decent description of using schemas within models in mongoose and why you would want to do it, and also shows you how to push tasks via the model while the schema is all about the structure etc.
Original Post:
Let’s start with a simple example of embedding a schema inside a model.
I created a new
TaskSchemaobject with basic info a task might have. A Mongoose virtual attribute is setup to conveniently combine the name and priority of the Task. I only specified a getter here but virtual setters are supported as well.I also defined a simple task method called
isHighPriorityto demonstrate how methods work with this setup.In the
ListSchemadefinition you’ll notice how thetaskskey is configured to hold an array ofTaskSchemaobjects. Thetaskkey will become an instance ofDocumentArraywhich provides special methods for dealing with embedded Mongo documents.For now I only passed the
ListSchemaobject intomongoose.modeland left theTaskSchemaout. Technically it’s not necessary to turn theTaskSchemainto a formal model since we won’t be saving it in it’s own collection. Later on I’ll show you how it doesn’t harm anything if you do and it can help to organize all your models in the same way especially when they start spanning multiple files.With the
Listmodel setup let’s add a couple tasks to it and save them to Mongo.The tasks attribute on the instance of our
Listmodel (sampleList) works like a regular JavaScript array and we can add new tasks to it using push. The important thing to notice is thetasksare added as regular JavaScript objects. It’s a subtle distinction that may not be immediately intuitive.You can verify from the Mongo shell that the new list and tasks were saved to mongo.
Now we can use the
ObjectIdto pull up theSample Listand iterate through its tasks.If you run that last bit of code you’ll get an error saying the embedded document doesn’t have a method
isHighPriority. In the current version of Mongoose you can’t access methods on embedded schemas directly. There’s an open ticket to fix it and after posing the question to the Mongoose Google Group, manimal45 posted a helpful work-around to use for now.If you run that code you should see the following output on the command line.
With that work-around in mind let’s turn the
TaskSchemainto a Mongoose model.The
TaskSchemadefinition is the same as before so I left it out. Once its turned into a model we can still access it’s underlying Schema object using dot notation.Let’s create a new list and embed two Task model instances within it.
As we’re embedding the Task model instances into the List we’re calling
toObjecton them to convert their data into plain JavaScript objects that theList.tasksDocumentArrayis expecting. When you save model instances this way your embedded documents will containObjectIds.The complete code example is available as a gist. Hopefully these work-arounds help smooth things over as Mongoose continues to develop. I’m still pretty new to Mongoose and MongoDB so please feel free to share better solutions and tips in the comments. Happy data modeling!