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
15
votes
4 answers

In mongoose how do I require a String field to not be null or undefined (permitting 0-length string)?

I have tried this, which allows null, undefined, and complete omission of the key to be saved: { myField: { type: String, validate: value => typeof value === 'string', }, } and this, which does not allow '' (the empty string) to be…
binki
  • 7,754
  • 5
  • 64
  • 110
15
votes
6 answers

Sharing data between Mongoose middleware methods pre save and post save

SEE UPDATED EXAMPLE CODE @ BOTTOM I'm using Mongoose (which is awesome btw!) in my current NodeJS projects, and I have a MDB collection thats going to store the changes of documents in a different collection (Basically a changelog storing what was…
Justin
  • 1,959
  • 5
  • 22
  • 40
15
votes
3 answers

Skip timestamps middleware for certain updates in Mongoose

My application uses Mongoose and has a schema that uses the timestamps option: var fooSchema = new Schema({ name: String, }, { timestamps: true, }); mongoose.model('Foo', fooSchema); So whenever an update is written to the collection, the…
str
  • 42,689
  • 17
  • 109
  • 127
15
votes
1 answer

Best way to maintain only one schema between Mongoose and Joi

I'm using Hapi to develop a web service, with Mongoose as ODM and Joi as validator. I really like Joi's validation and the way it connects with HAPI (I need Joi's description function to display some description in swagger) but I don't want to…
Manuel Bitto
  • 5,073
  • 6
  • 39
  • 47
14
votes
4 answers

how to reuse mongoose model in multiple projects in the elegant way

Let's say I have 3 node.js projects(1 app backend, 1 app-admin backend, 1 analysis api). In each projects, I have a model schema call loan. { attributes: { userId: { type: String, required: true, index: true, ref: 'users', comment: '用户id' }, …
no7dw
  • 505
  • 5
  • 13
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
14
votes
1 answer

update schema in mongoose to add new property

I am trying to update a schema to add a new property field. I was hoping it would be as simple as adding the property to the schema with the updated field being accessible. I have an existing schema let userDrinkSchema = new mongoose.Schema({ …
Winnemucca
  • 3,309
  • 11
  • 37
  • 66
14
votes
2 answers

Easy way to increment Mongoose document versions for any update queries?

I want to start taking advantage of Mongooses document versioning (__v key). I was having an issue actually incrementing the version value, then I found that you have to add this.increment() when executing a query. Is there a way to have…
Justin
  • 1,959
  • 5
  • 22
  • 40
14
votes
1 answer

how to create mysql schema in nodejs

I am using nodejs, express framework and mysql for my database. I would like to know how can I replicate this mongoose code using mysql. I cannot find a way to write my sql schema in a nodejs file. Do I have to use workbench for this? Thanks! var…
Safwan Ull Karim
  • 652
  • 5
  • 10
  • 20
13
votes
1 answer

Node JS mongoose create a blog post commenting system

I am trying to build a simple blog with a commenting system with mongoose and express. There is no issse of creating and posting blogs here and each post can be displayed correctly. However, there are some issues associated with comments and each…
Kingsfull123
  • 483
  • 1
  • 6
  • 12
13
votes
4 answers

How do I define a different primary key other than _id in Mongoose?

I want to define Mongoose schemas with primary keys that are not _id. The documentation says it only allows the schema options flag _id to be set to false in subdocuments. Also, I want the primary key to be a String and not an ObjectId. Is that…
douira
  • 506
  • 1
  • 6
  • 22
13
votes
1 answer

Adding child document to existing mongodb document

Essentially I am just trying to add a new sub-document to my existing mongodb document that has the following schema /models/server/destination.js // this is the "destination" model for mongoose var mongoose = require('mongoose') var Adventure =…
pwborodich
  • 382
  • 6
  • 22
12
votes
1 answer

Mongoose require a field based on some enum values of another field

I have a Mongoose schema Employee. In that I want to store a field (phone number) related to office for the employee, only if he/she is eligible for office, which is only for two levels "senior" and "c-level". The schema: const mongoose =…
Wasif Ali
  • 886
  • 1
  • 13
  • 29
12
votes
6 answers

MongoDB E11000 duplicate key error

I have a model that keeps erroring out after the first POST. I'm creating a scheduling application, which is X number of days, with rooms, and time slots for the rooms. The issue I'm having is creating Day Objects in the database. For sake of easy…
wsfuller
  • 1,810
  • 8
  • 31
  • 49
12
votes
1 answer

Virtuals in mongoose, 'this' is empty object

ok, i'm new to mongoose and trying to understand how to use virtual properties. this is a sample code that i've been testing. var mongoose = require('mongoose'); var Schema = mongoose.Schema; var objSchema = new Schema({ created: {type: Number,…
Shahriar HD
  • 123
  • 1
  • 6