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

Mongoose specify optional array of objects

One of the keys in my mongo collection is options: [ new mongoose.Schema( { answer: { type: String, required: true, }, value: { type: Number, min: -10, …
DanMossa
  • 994
  • 2
  • 17
  • 48
4
votes
2 answers

Mongoose: filter data if value exists in array within array of objects

I had a working mongoose query using Aggregate where I filter data based on a value inside an array of objects and am running into trouble due to change in requirements. This is my how my data looks like in MongoDB (it has been filled with dummy…
4
votes
1 answer

Resolve Promise in mongoose virtual property?

I'm using asynchronous virtual properties to count how often that document has been referenced in a specific other collection. This feature has been added. // Schema mortician.js const Pickup = require('./pickup') const mongoose =…
Tomblarom
  • 1,429
  • 1
  • 15
  • 38
4
votes
2 answers

use mongoose schema over multiple microservices

my application is split into multiple microservices that are running on heroku dynos (they can't access each others files). Sometimes, there are multiple microservices working with one collection. Therefore, both of the microservices need the…
linus_hologram
  • 1,595
  • 13
  • 38
4
votes
1 answer

Use function in `required` field in mongoose.Schema using TypeScript

I'm trying to create a dynamic schema with fields being required based on the value of another field. Sample Schema: const foo = new Schema({ status: { type: String, default: "in_process" }, route: { type:…
cankentcode
  • 460
  • 1
  • 5
  • 20
4
votes
0 answers

Mongoose by default saving floats with zero after decimal as integers ( 0.0 as 0 )

I have a schema-less Model i.e. a blank schema as : const NewSchema = new mongoose.Schema({ }); The reason I need this is, I am expecting unstructured and unexpected fields as of now during the development phase of my project. var myModel = new…
Piyush Upadhyay
  • 177
  • 1
  • 12
4
votes
2 answers

how to make fixed size arrays inside array mongoose schema

I have example input [[1,2],[3,2],[1,3],...,[4,5]] How to write model schema in mongoose? This is my Schema const SubproductSchema = new Schema({ ... positions: [{ type: [Number], validate: { validator: function(value){ …
4
votes
2 answers

How to reference another property in your mongoose schema

If I have a property in my schema that depends on another one (like it's minimum value), how do define that in my schema? I have an endDate and an actualEndDate properties in my schema, the second one will always be greater than or equal to the…
alkhatim
  • 353
  • 1
  • 10
4
votes
4 answers

Time format in mongoose scheema Object

I have schema object like below schedule: [{ time: { type: Date, required: true } }] and when I try to post data in postman as "schedule":[ { "time":"18:00:00" }] I'm getting…
change need
  • 137
  • 2
  • 6
  • 23
4
votes
8 answers

Mongoose validation err: Invalid schema configuration

Trying to model a relationship between collections by embedding documents but when validating in the schema and setting "required" to True, here comes the err once I comment the required in genre object in movies schema the problem is solved but I…
Omid
  • 301
  • 1
  • 2
  • 12
4
votes
1 answer

How to define array of objects in side nested array mongoose

I have an express API using an already populated mongoDB and have defined the schema like so: const accountHolderSchema= new mongoose.Schema({ pid: {Type: Number}, accountNumber: {type: String}, relationshipType: {type: String}, firstName:…
O'Dane Brissett
  • 1,284
  • 1
  • 8
  • 23
4
votes
1 answer

Can I use MongoDB schema model for defining IndexedDB indexes?

I am creating a progressive web app that that is using NodeJS and Express as backend, MongoDB as server, and IndexedDB for storing data locally when offline. Currently I have defined some Mongoose schema models, and my application is suppose to…
4
votes
1 answer

mongoose nested schemas for multiple array of objects

I am trying to build a rest api with node, express and mongodb for webapp where I have three routes or section on frontend Quotes Stories News When user will click Quotes he will see only quotes, same will goes with Stories and News. I am…
Basit
  • 327
  • 3
  • 8
  • 19
4
votes
1 answer

Can't extract geo keys, unknown GeoJSON type: { coordinates: [ 13.42493130000003, 52.50074619999999 ]

I have this form where I get the info of a user, the user has been saved with a location correctly. But when i try to update it, it gives me this error back: > Can't extract geo keys: { _id: ObjectId('5c4eb6c94f59c09ee7490ee0'), > salt: >…
user2643810
4
votes
3 answers

How to work with Date and Time in NodeJS, Mongoose and TypeScript?

I come from the java world and I'm starting in NodeJS. I'm having a hard time understanding how to work with dates and times in NodeJS. Only dates and only hours. Here is an example: export interface teste extends mongoose.Document { …