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

Does node.js server need internet connection to run?

I disable the internet connection and run the node server npm start, then it throws the error: And now enabling internet connection and running the server works fine: So, I want to confirm if this really needs to be connected to the internet while…
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
8
votes
1 answer

Typescript and Mongoose: Property 'x' does not exist on type 'Document'

This is my Mongoose model that I use together with TypeScript: import mongoose, { Schema } from "mongoose"; const userSchema: Schema = new Schema( { email: { type: String, required: true, unique: true, lowercase:…
mrks
  • 5,439
  • 11
  • 52
  • 74
8
votes
2 answers

choose the mongoose schema on group chat application?

i am going to design a group chat application based on mongodb, there are two schema design choices, one is designed as one document for one group chat message, another is designed as one document for all group messages. In the first option, it can…
user824624
  • 7,077
  • 27
  • 106
  • 183
8
votes
2 answers

How to create mongoose schema with array of objects

I have this json: { "data": [ "id": "1", "name": "Sample test", "description": "this is a sample test", "category": "tests", "points": 100, "startDate"​:​"2018-02-15 00:00:00"​, …
8
votes
2 answers

Mongoose - Cannot populate with sort on path notifications.from because it is a subproperty of a document array

I have a very simple mongo scheme I'm accessing with mongoose I can map the username and firstname to each notification's from field by using populate, the issue is I can't seem to get any sorting to work on the date field With this code I get an…
totalnoob
  • 2,521
  • 8
  • 35
  • 69
8
votes
2 answers

How to get a list of available Mongoose Discriminators?

Given a situation where you have a User Scheme that you use to create a base model called User. And then for user roles, you use mongoose discriminators to create inherited models called Admin, Employee and Client. Is there a way to programmatically…
8
votes
3 answers

How to trigger a function whenever a mongoose document is updated

I have a user model schema in mongoose which contains a list of friends and groups and stats info like so... var user = new Schema({ email: { type: String, required: true, unique: true }, password: { type: String, required: true, select: false…
CSharp
  • 1,396
  • 1
  • 18
  • 41
8
votes
2 answers

Specify an Index Name with Mongoose

When defining a Mongoose schema it is often prudent to specify what indexes should exist. That said, in many cases we would like to control the name of the index created, especially when those indexes are compound so they are understandable. …
Don Scott
  • 3,179
  • 1
  • 26
  • 40
8
votes
6 answers

Mongoose.js findOne returning query metadata

I am attemping to run a mongoose.findOne on my data base but I get unexpected results.My query is const User = mongoose.model('User', {name: String, email: String, passwordHash: String, validation: String, validationCode: String, favorites: Array…
Jack Tidbury
  • 183
  • 1
  • 1
  • 12
8
votes
2 answers

Storing site config as Mongoose model

I have website configuration (currently stored as JSON file), and I would like to move it to MongoDB and probably use Mongoose to handle read-write operations and perform validation through schemas. Configuration is an object with limited amount of…
Estus Flask
  • 206,104
  • 70
  • 425
  • 565
8
votes
4 answers

How to register mongoose plugin for all schemas?

What am I missing? The Mongoose docs say that mongoose.plugin() registers a plugin for all schemas. This is not working. I CAN register my plugin on EACH schema. My plugin: module.exports = function (schema, options) { …
G. Deward
  • 1,542
  • 3
  • 17
  • 30
8
votes
1 answer

Mongoose advanced custom schema object type

I couldn't find any example of an advanced custom schema type involving custom objects (or value-objects) in Mongoose >=4.4. Imagine that I want to use a custom type like: function Polygon(c) { this.bounds = [ /* some data */ ]; this.npoints =…
Dario
  • 3,905
  • 2
  • 13
  • 27
7
votes
2 answers

React-hook-form useFieldArray for an array of strings instead of objects

I am using useFieldArray to fetch default values from my backend api. My categories is an array of strings. However, react-hook-form only supports array of objects. Here is my mongoose's schema type BookDocument = Document & { title: string …
Finlandary
  • 105
  • 1
  • 8
7
votes
1 answer

mongoDB returning with $numberDecimal in the response of a query

Below is my mongoose schema const mongoose = require('mongoose'); const AccountingCostsSchema = mongoose.Schema( { // other properties goes here accountCosts: { type: mongoose.Decimal128, set: (v) => …
The JOKER
  • 453
  • 8
  • 21
7
votes
1 answer

How can remove _id from embedded object in Mongoose schema?

I have this Mongoose schema: const User = mongoose.model('User', new Schema({ id: String, name: String, extra: { bb: Number, chain: Number } }), 'users'); When I see a new User, it's stored like this: { _id: ..., id: '1234', …
Héctor
  • 24,444
  • 35
  • 132
  • 243