Find specific model

Hello, I am working on my anon message board. My data structure in mongoose is as follows:

const boardSchema = new Schema({
  thread: String,
  dateCreated: {type: Date, default:},
  notes: [{
    note: String,
    replies: [{note: String, date: {type: Date, default:} }]

for each “board” (which will be a model or document) there are multiple threads and within each thread there are multiple notes and replies …

My question is how do I query based on Model?
if I create a model ‘bean’ and then later create model ‘tea’ by:
const bean = new Schema(‘bean’, boardSchema);
const tea = new Schema(‘tea’, boardSchema);

how do I later access each of these models with Model.update(…);
, keeping in mind that “bean” and “tea” are not kept in memory?

I’m not sure if I’m missing something, so forgive me if my response doesn’t match your question.

Once you’ve defined a schema, then you define a model based on that schema. A model is a constructor which allows you to create and read database documents. A document is an instance of a model.

For example,

We can create a customized schema called blogSchema.

  var mongoose = require('mongoose');
  var Schema = mongoose.Schema;

  var blogSchema = new Schema({
    title:  String,
    author: String,
    body:   String,
    comments: [{ body: String, date: Date }],
    date: { type: Date, default: },
    hidden: Boolean,
    meta: {
      votes: Number,
      favs:  Number

Then we can create a model based on that schema (blogSchema)

var Blog = mongoose.model('Blog', blogSchema);

This will automatically create a collection called blogs. The default collection naming convention is to take the first letter of the model and make it lowercase (if it isn’t already) and pluralize it by appending an s.

So, the Model.updateOne() method will be for querying the database and updating a document in a collection. In our case, the collection will be blogs and we query like so:


Example code taken from Mongoose Docs.

Thanks a lot for your answer. Your answer makes sense. However, say you have created “BlogA” as a collection and you have created “BlogB” as another collection.

Now you want to access each blog to read/update it’s values.
For example each blog will contain it’s own material, such as “Topics”. All materials within each blog (collection) are private.
How would you access from the main database JS class each blog created?

Blog variables (“BlogA”) will be created by action listeners, so they won’t necessarily be cached within JS; for e.g.;

back-end post request

    .post(function(req, res) {
      compiledObject = {
        board: req.body.boardName,
        thread: req.body.thread,
        note: req.body.note
db = new DB();
      db.newBoard(compiledObject.board, compiledObject.thread); 

database.js (exported as DB):

newBoard(boardName, threadTitle) {
  this.boardName = boardName;
  const Board = mongoose.model(boardName, boardSchema);
  let newB = new Board({board_title: this.boardName, thread: threadTitle});, result) {
    if (err) {

Looks like you can retrieve your model using the same command you used to build it^^
So if you want to update something into Tea model:
const Tea = mongoose.model("Tea");

Mongoose API docs - Model

You can also have a look at the method below in the docs, ModelNames(), which returns a list of all models created on that mongoose instance^^
Maybe it could be of some use in your setup^^

Cool! thanks that was really useful.

1 Like