Mongoose Models are constructors compiled from Schema definitions. They are responsible to creating and editing documents from MongoDB.
Questions tagged [mongoose-models]
50 questions
14
votes
1 answer
Mongoose static Model definitions in Typescript
I created a Mongoose Schema and added some static methods for the Model, named Campaign.
If I console.log Campaign I can see the methods present on it. The problem is I don't know where to add those methods so that Typescript is also aware of…

Daniel Nitu
- 387
- 3
- 8
7
votes
1 answer
Creating mongoose models with typescript - subdocuments
I am in the process of implementing mongoose models with typescript as outlined in this article: https://github.com/Appsilon/styleguide/wiki/mongoose-typescript-models and am not sure of how this translates when you are working with arrays of…

user1790300
- 2,143
- 10
- 54
- 123
4
votes
1 answer
difference between Query and Model mongoose
these days i'm trying to learn more about mongoose to implement it in my project ,while going through the doc , i saw both Model and Query, both have many methods in common ,my question is what is the difference between them for…

Alexander Pato
- 41
- 2
2
votes
1 answer
How to make a mongoose schema of arrays with nested subdocuments
I want to store an array of nested subdocuments such as the one down bellow:
[
{"2021-02-01income":{"value":37.95,"tax":0,"type":"income"}},
{"2021-03-01income":{"value":38.25,"tax":0,"type":"income"}},
{"2021-03-08fund":…

lucasbbs
- 349
- 4
- 14
2
votes
2 answers
Capitalization of the model name (Mongoose)?
Why the name of the Model is Capitalized.
As in their documentation, they have capitalized it.
var schema = new mongoose.Schema({ name: 'string', size: 'string' });
var Tank = mongoose.model('Tank', schema);
Why is Tank capitalized here? Is there…

Sanyam
- 64
- 3
- 11
2
votes
3 answers
How to Connect/Query Mongoose to an existing Database
I am working on trying to connect a Node/Express server to an existing MongoDB Database/Collection. I have already successfully connected to the database. However, I am having a tremendously difficult time setting up my models/schema to query.
The…

FDRH
- 209
- 3
- 11
2
votes
1 answer
Mongoose model query(findById) error in hooks(pre middleware) due to foreign model
In this case I have two models: Protocol and Comment. Each model has a middleware('pre' or 'remove') that calls the other model. Calling the Comment middleware in Comment.js stops at Protocol.findById() because Protocol is an object instead of a…

Jérémie Touzé
- 56
- 6
2
votes
1 answer
Mongoose Error - Mongoose models with same model name
I am working on a NodeJs application and I am using mongoose node package.
Sample Code
I am using following method to create dynamic collections and these collections sometimes fail to persist the data in database -
const Mongoose =…

planet_hunter
- 3,866
- 1
- 26
- 39
1
vote
2 answers
Mongoose Models: Cannot read properties of undefined (reading 'findOne')
I try to import a model from the file where I create it (the same file where I connect the database) and when I try to work with it, it is "undefined".
const { model } = require("../database");
model.findOne(. . .).then(. . .
This is the file I…

Zac Feng
- 37
- 6
1
vote
1 answer
How can I use the mongoose model without initializing in the constructor in nestjs?
I was using the mongoose schema injection in the service as we normally do. But now i wish to transfer some of my code to a helper function so that it can be reused.
Generally, mongoose schema is injected as:-
export class UsersService {
…

Rahul Sharma
- 11
- 1
1
vote
1 answer
How can The Model class be a subclass of the Document class?
The underline statement does not make any sense to me. I believe mongoose devs didn't publish their docs wrong and I'm missing a point.
console.log(mongoose.Model instanceof mongoose.Document)
Also when I run the code above, it outputs false.
Can…

Özgür
- 41
- 4
1
vote
2 answers
MongoDB - is there a better way to store a list of ObjectIDs?
Say I have a User schema/model and the user has a list of friends. Mongoose wants you to store your list of friends (foreign key / ObjectID type) as an array, right? Which means if I want to find my friend by ID, Mongoose will search the array until…

Justin Jaeger
- 181
- 1
- 6
1
vote
1 answer
What is third parameter in mongoose.model? What is use of it
const db = mongoose.model('User', userSchema, 'user')
What is use of that third parameter?
Where can we use that parameter. What is use of that parameter. Any practical example

Rohit Hushare
- 61
- 7
1
vote
0 answers
How to create complex nested documents using mongoose?
I want to create a complex nested document which can store values like this
category: {
"fish": ["Sardines", "Goldfish"],
"dogs": ["German Shepherd", "Dobberman"]
}
Here's what I tried
export const CategorySchema = new mongoose.Schema(
{
…

onMyWay
- 117
- 1
- 1
- 7
1
vote
1 answer
Mongoose: Cannot read property 'where' of undefined, _id
I've got that model:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var uniqueValidator = require('mongoose-unique-validator');
var EquipmentSchema = new Schema({
model: { type: String, required: true, unique: true},
…

Martin Carre
- 1,157
- 4
- 20
- 42