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
7
votes
1 answer

Mongoose - ObjectID as key?

I would like to have an object("ingredients" in example) in my mongoose model, where the keys are ObjectIDs, and their values are numbers. Is it possible to do so? How should I define my mongoose schema? You can find an example below. Example JSON: …
k6pib6r6
  • 119
  • 2
  • 5
7
votes
3 answers

How to store URL value in Mongoose Schema?

I am uploading the images from an IOS application to Firebase which returns to me the metadata including the URL of type URL. Should I store it of type String in the database like below code? or there is specific type for URLs? var schema = new…
Source
  • 99
  • 1
  • 1
  • 7
7
votes
1 answer

Mongoose Validation Based on Other Fields

Consider the following schema for saving time intervals in Mongoose: let dateIntervalSchema = new mongoose.Schema({ begin: { type: Date, required: true }, end: { type: Date, required: true } }) How can I ensure that end is always greater than…
Kayvan Mazaheri
  • 2,447
  • 25
  • 40
7
votes
2 answers

How to populate documents with unlimited nested levels using mongoose

I'm designing a web application that manages organizational structure for parent and child companies. There are two types of companies: 1- Main company, 2 -Subsidiary company.The company can belong only to one company but can have a few child…
7
votes
4 answers

How to solve cannot read property 'push' of undefined in nodejs application?

While I am adding values to a sub document, in command prompt it shows the error like cannot read property push. How can I solve this? Here's my schema code with the help of these I gave values to parent schema but I'm not able to give the…
Syed Ayesha Bebe
  • 1,797
  • 1
  • 15
  • 26
7
votes
0 answers

Retain Key Order in Mongoose/MongoDB

I want to keep keys order in mongodb strictly as its defined in schema. I am using mongodb 3.0.14 and mongoose 3.9.7 . As in Mongoose >=4.6.4 retainKeyOrder property is working to maintain key order. More Detail How i can achieve this earlier…
Muhammad Usman
  • 345
  • 1
  • 11
7
votes
1 answer

How to create a Mongoose schema from JSON

I am new into mongodb, nodejs and mongooseJS. Lately, I have been trying to create a mongoose schema for my JSON. { "endpoints":["a","z"], "poi":[{ "location_name": "a", "latitude": " 10.1075702", "longitude": "76.345662", "distance" :…
6
votes
1 answer

Property '_id' does not exist on type. Getting type error when trying to access property _id on result of a promise in nestjs application

In my nest application I am getting type error when calling _id on user because mongoose defines the _id automatically & therefore its not present in my schema which is defined as type for the promise. When the promise type is changed to any like…
6
votes
2 answers

NestJS + Mongoose schema with a custom typescript Type

I'm Trying to create a Mongo Schema, using nestjs/mongoose decorators, from the following class: @Schema() export class Constraint { @Prop() reason: string; @Prop() status: Status; @Prop() time: number; } The problem is Status is…
dod_moshe
  • 340
  • 2
  • 13
6
votes
1 answer

Inheritance method "virtual" in extend schema mongoose doesn't work

I have below method in model UserSchema: userSchema.virtual('password') .set(function(password) { this._password = password; this.salt = this.makeSalt(); this.hashed_password = this.encryptPassword(password); …
Umbro
  • 1,984
  • 12
  • 40
  • 99
6
votes
1 answer

How to do mongoose model self reference on Typescript?

I have a model: const comment = new mongoose.Schema({ id: { type: ObjectId, required: true }, comment: { type: String }, replies: [comment] }); Want to create document like this: { "id": 1, "comment": "Grand Parent Comment", "replies":…
6
votes
3 answers

mongoose different ways to reference subdocuments?

This syntax is straight from the mongoose documentation on subtypes. However, I've also seen this alternate reference to subdocs. What is the difference? https://mongoosejs.com/docs/subdocs.html var childSchema = new Schema({ name: 'string' }); var…
Bill
  • 915
  • 2
  • 13
  • 23
6
votes
1 answer

In mongoose set a field based on the value of another field in findOneAndUpdate

I'm working on a project where in one model I need to set the value of a field based on another fields value. Let me explain with some code. Destination model const mongoose = require('mongoose'); const Schema = mongoose.Schema; const…
vice_versa
  • 99
  • 1
  • 9
6
votes
2 answers

mongoose conditional required field validation

According to mongoose built-in validators documentations, I can use conditional required field: const schema = mongoose.Schema({ a: { type: String, required: function () { return this.b === 1 } }, b:…
Ron537
  • 990
  • 1
  • 9
  • 20
6
votes
2 answers

Why does mongoose populate virtual field as array instead of single item?

I want to populate an object into a virtual field with mongoose as a JSON object, but it always returns an array with a single item. Here is my scheme code (part with virtual field): Order.virtual('client', { type: 'ObjectId', ref: 'User', …