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
11
votes
2 answers

VS code auto compete not working properly when creating a mongoose schema

Any idea why the intellisense is completely useless when I create a mongoose schema? Even tho it used to work in the past. I have searched a lot about this topic but unfortunately couldn't find anything that can help. Node.js related extensions I…
Zeyad Shaban
  • 896
  • 1
  • 8
  • 23
11
votes
1 answer

Mongoose model required and with default

I created mongoose model with a field named "phoneNumber": ... phoneNumber: { type: 'String', required: true, default: '' }, ... Whenever I create a new record of that model, I got validation failed exception: Path `phoneNumber` is…
Naor
  • 23,465
  • 48
  • 152
  • 268
11
votes
1 answer

Mongoose - add global method to all models

Simple question: How can I add static methods to my models in Mongoose, that applies to every model instead of just one?
Alex Wohlbruck
  • 1,340
  • 2
  • 25
  • 41
11
votes
5 answers

How to use mongoose model schema with dynamic keys?

i'm using mongoose with nodejs, and i need to create a dynamic schema model, this is my code: schema.add({key : String}); key = "user_name", but in my db i found that the model take it as key { key : "Michele" } and not { user_name:…
Ahmed Commando
  • 723
  • 2
  • 7
  • 23
10
votes
9 answers

(node:71307) [DEP0079] DeprecationWarning

Try to update MongoDB document Getting Deprecation Warning as (node:71307) [DEP0079] DeprecationWarning: Custom inspection function on Objects via .inspect() is deprecated Node version v10.5.0, db version v3.6.5, Mongoose version…
Kiran Ghatage
  • 407
  • 1
  • 8
  • 15
10
votes
1 answer

How do I perform a find query in Mongoose?

i have a collection of Ebooks data in mongodb like { "_id" : ObjectId("58b56fe19585b10cd42981d8"), "cover_path" : "D:\\Ebooks\\uploads\\ebooks\\cover\\1488285665748-img1-700x400.jpg", "path" :…
10
votes
7 answers

MongoDB / Mongoose timestamps not updating

Schema: var schema = new Schema({...}, { timestamps: true, id: false, toJSON: { virtuals: true, }, toObject: { virtual: true, } }); schema.virtual('updated').get(function () { if(typeof this.updatedAt ===…
Simon Poole
  • 493
  • 1
  • 6
  • 15
10
votes
3 answers

Mongoose Schema with nested optional object

Using the following schema: { data1: String, nested: { nestedProp1: String, nestedSub: [String] } } When I do new MyModel({data1: 'something}).toObject() shows the newly created document like this: { '_id' : 'xxxxx', 'data1':…
Sunny Milenov
  • 21,990
  • 6
  • 80
  • 106
10
votes
1 answer

mongoose : Could we populate reference object to a alias/virtual property?

I'm looking for a way to populate to a alias/virtual property using mongoose ? For now, mongoose's population is add more fields to the reference property (normally is the identity key). I want to keep my original property and populate the…
pham cuong
  • 849
  • 1
  • 12
  • 24
10
votes
2 answers

Best practice to validate Mongoose Schema and display custom error message

I have started learning Node.js and one thing which is a little bit confusing to me is Schema validation. What would be the best practice to validate data and display custom error message to user? Let's say we have this simple Schema: var mongoose =…
Astagron
  • 277
  • 4
  • 13
9
votes
2 answers

How to get the defined indexes from Mongoose

I have been trying to find out the indexes which are already created via MongoDB manually( I have created 2d sphere indexes for two fields via mongobooster and creating one via schema by defining it). Now if i run this query in mongodbooster…
Kannan T
  • 1,639
  • 5
  • 18
  • 29
9
votes
1 answer

Mongo / Mongoose: Does Mongoose automatically create indexes on ObjectId types?

I may have missed this in the mongo index documentation or in the mongoose documentation. Suppose I have a mongoose schema as such: const SomeEntity = new Schema({ foo: { type: String, required: true }, bar { type: Schema.ObjectId, ref:…
dipole_moment
  • 5,266
  • 4
  • 39
  • 55
9
votes
5 answers

Mongo schema, array of string with unique values

I'm creating the schema for a mongo document and I can do everything except prevent duplicates in a non-object array. I'm aware of the addToSet, but I'm referring to Mongo Schema. I don't want to check on Update using $addToSet, rather I want this…
Proximo
  • 6,235
  • 11
  • 49
  • 67
9
votes
1 answer

How to use a mongoose model defined in a separate file if the file is not exported?

Consider a very simple Express 4 app structure: -- app.js -- models |--db.js |--news.js where news.js contains a mongoose schema and a model based on that schema: var mongoose = require('mongoose'); var Schema = mongoose.Schema; var…
Aren Li
  • 1,852
  • 1
  • 15
  • 14
9
votes
1 answer

Mongoose select: false not working on location nested object

I want the location field of my schema to be hidden by default. I added select: false property to it, but it is always returned when selecting documents... var userSchema = new mongoose.Schema({ cellphone: { type: String, required: true, …
Scaraux
  • 3,841
  • 4
  • 40
  • 80