Questions tagged [mongoose-schema]

Everything in the Mongoose ODM starts with a Schema. Each schema maps to a MongoDB collection and defines the shape of the documents within that collection.

Everything in the Mongoose ODM starts with a Schema. Each schema maps to a MongoDB collection and defines the shape of the documents within that collection.

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: Date.now },
  hidden: Boolean,
 meta: {
  votes: Number,
  favs:  Number
 }
});

Related Links

3348 questions
5
votes
2 answers

Mongoose changes password every time I save with pre-save hook

I'm using a pre-save hook with bcrypt to encrypt passwords on the system. It works fine when creating or changing a password. The problem is that it seems to re-encrypt the password every time I change and save a different field, for example…
Chris
  • 1,863
  • 5
  • 30
  • 43
5
votes
2 answers

Mongoose accepts null for Number field

I have a mongoose schema where I'm storing a port number. I also have a default value set for the field. port:{ type:Number, default:1234 } If I don't get any value via my API, it gets set to 1234. However, If someone sends null, it accepts…
Dushyant Bangal
  • 6,048
  • 8
  • 48
  • 80
5
votes
3 answers

Returning specific fields with mongoose

I'm trying to accomplish something really easy but still manage to fail. What I am trying to do is when I get a get request on my server I want to return all documents BUT just the specific fields populated. My schema goes as follows var…
Jack Wolther
  • 121
  • 1
  • 2
  • 10
5
votes
3 answers

Calculation of third field based on two other field in Mongoose

I have 2 fields on on Mongoose model: totalAttempts and totalRight. I am planning to calculate accuracy based on them. totalAttempts: { type: Number, default: 1 }, totalRight: { type: Number, default: 1 } This is what I have done…
Sandip Subedi
  • 1,039
  • 1
  • 14
  • 34
5
votes
1 answer

Array of subdocuments in Mongoose

I have two schemas. FAQ Schema const EventFAQSchema = mongoose.Schema({ question: { type: String, req: true }, answer: { type: String, req: true } }); EventSchema const EventSchema = mongoose.Schema({ "title": { type: String }, …
Giridhar Karnik
  • 2,213
  • 4
  • 27
  • 47
5
votes
1 answer

How to avoid writing the Schema of Mongoose twice when using it with TypeScript?

I am trying to use Mongoose together with TypeScript. With JavaScript alone, we can pretty easily write the schema for a model Foo. const fooSchema = new mongoose.Schema({ name: String, value: Number }); fooSchema.methods.someInstanceMethod =…
Zsw
  • 3,920
  • 4
  • 29
  • 43
5
votes
4 answers

Mongoose Promise error

This is the error which is still being thrown when saving even after adding native promise. (node:5604) DeprecationWarning: Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead:…
Hari Aakash
  • 112
  • 2
  • 13
5
votes
2 answers

How to share Mongoose Schemas between applications

I have an application with the following structure: -MyApp -backend -app +mongoose-models +node_modules index.js package.json -admin +(lots of stuff) -frontend -app +app_controllers +node_modules …
Anderson Ivan Witzke
  • 1,537
  • 1
  • 15
  • 24
5
votes
5 answers

Movie is not a constructor - Mongoose

I'm using the MEAN stack and trying to save data to my MongoDB database with Mongoose. However, I keep getting the error "Movie is not a constructor" for my model. But this is how the Mongoose docs show it, so i'm not sure what i'm doing…
Isaac G.
  • 51
  • 1
  • 4
4
votes
2 answers

NestJS - Nested polymorphism in sub schema

In NestJs, I am trying to let mongoose create a document based on a schema of which one of its properties can have different types. However, when I am saving the document, all the properties related to a specific type are lost. What am I missing?…
ant_hony
  • 81
  • 6
4
votes
2 answers

TypeError: Cannot read property 'Symbol(mongoose#Document#scope)' of undefined

This the error: collection is a reserved schema pathname and may break some functionality. You are allowed to use it, but use at your own risk. To disable this warning pass supressReservedKeysWarning as a schema…
Salman
  • 209
  • 1
  • 3
  • 7
4
votes
2 answers

What are all the options that a field can have in a mongoose Schema?

So basically I am working with mongodb using mongoose. I would like to know what are all the potential options that a field can have in a mongoose schema. For example, a user schema: const UserSchema = new Schema( { username: { …
darkstar
  • 829
  • 11
  • 23
4
votes
1 answer

TypeError: this.$__setSchema is not a function

I am trying to insert data into mongodb at mongodb cloud. But while running the code i am getting error like TypeError: this.$__setSchema is not a function. //Function Called this.$__setSchema(_schema); //Function…
4
votes
1 answer

What should be the type of a field in schema if "any" cannot be used in Nest.js?

I'm working in Nest.js using Mongoose. While creating schema, I have a field extra_options which can store any type of value (array, object, string etc). Keeping its type as "any" does not work. What should be the correct type? Here is a fragment of…
4
votes
1 answer

MongoDB/Mongoose Updating Whole Sub/Nested Document

I have 2 schema User Schema const usersSchema = new schema({ name: { type: String, required: [true, "name is required"], unique: true }, email: { type: String, required: [true, "email is…
Jsennin
  • 153
  • 2
  • 12