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
0 answers

Push or addToSet for multiple items in subdocument in Mongoose

Is it possible to use Push or addToSet for multiple items, for example this code add's one note if it doesn't exist yet, in a subdocument array for user. But what if I have 200 notes? const note = { id: 1 }; User.update( {name: 'admin',…
Miguel Stevens
  • 8,631
  • 18
  • 66
  • 125
4
votes
0 answers

how to fix a mongoose strictmode error on an updateOne?

i have a model like this : Mongoose Schema var LinkedinUpdateSchema = new mongoose.Schema({ likes:{values:[PersonSchema], _total: Number}, numLikes: Number, timestamp: Number, updateComments:{ values:[CommentSchema], _total:…
4
votes
3 answers

How to define array of Strings in mongoose?

I want to define a document as numbers : ["99995", "44444", "666664".....] The numbers shouldn't start with 0 and length should be 5. Also there should be minimum 1 element The mongoose schema that I defined in something of this type numbers: { …
Ankuj
  • 713
  • 3
  • 10
  • 27
4
votes
0 answers

mongoose - enable `toJSON` for specific virtuals

I have a mongoose model with a bunch of virtual properties, if I do model.toJSON({ virtuals: true }) all of them will be in the JSON. Is it possible for me to only do this for particular virtual properties? So instead of all of them a select few…
user3690467
  • 3,049
  • 6
  • 27
  • 54
4
votes
1 answer

can I add new property when create new doc in mongoose?

I have this schema (for example): var WordSchema = new Schema({ word: { type: String } }); module.exports = mongoose.model('Word', WordSchema); How I can add new property when create new doc in mongoose? something like this: let…
ramin
  • 89
  • 1
  • 11
4
votes
1 answer

Generate auto increment field using Mongoose?

I have a database which consist of four fields id, text, key, status. I want to add another field named order which consist of a number, which is representation of the order. I also have functionality to delete documents so after deletion order will…
4
votes
1 answer

Mongoose Schema statics vs methods

In a mongoose schema we can create the method as two way SchemaName.methods.fuctionName and SchemaName.statics.functionName. Statics is more easier to use I just need to call ModelName.Fuction. Methods need to create object to use. My question is…
Moe Thar
  • 41
  • 3
4
votes
2 answers

Error handling in async await

How can you implement error handling for Mongoose (current version v5.1.5)? For example, let's assume the following code where a user detail is being looked up. let u = await user.find({ code: id }).lean(); return u; And some error occurs, how…
Temp O'rary
  • 5,366
  • 13
  • 49
  • 109
4
votes
3 answers

How to get the parent of a schema in Mongoose?

I'm writing a Node.js application which uses Mongoose as an ORM. I have one model, called Event, and one schema, called Participant, which is stored inside of my Event schema as a subdocument. Problem is, I need to implement a method that should…
serge1peshcoff
  • 4,342
  • 11
  • 45
  • 76
4
votes
1 answer

When exactly does Mongoose's .pre('init') get called?

I want to create 'games' which each have their own unique access 'code'. The code is required in the schema, and I need to generate a code each time a new game is created. I thought schema.pre('init') would be a good place to generate this access…
Delta_HF
  • 43
  • 1
  • 3
4
votes
1 answer

Mongoose always return empty array?

Im new to nodejs.i already have a db named aqi with collection name pln. Im trying to show all the records in collection on web page but mongoose always returns empty array. i have tested it with other dbs but i can get data from them but for pln…
sarmad khan
  • 43
  • 1
  • 4
4
votes
0 answers

How to update all of an object's properties with spread syntax with Mongoose and Node

Thank you in advance, Im really stuck on this! I have a User object in the database like such: { "_id": { "$oid": "5a83470e722c1e00142e2216" }, "first_name": "Test", "last_name": "User", "password":…
4
votes
1 answer

mongodb schema how to set default value for array of sub documents?

Example I have this schema. { field1: { type: String, trim: true }, field2: [{ subField1: { type: String, }, subField2: { type: Number, default: 0 } }] } How should I set the default value on the…
zer09
  • 1,507
  • 2
  • 28
  • 48
4
votes
4 answers

Mongoose returning all fields regarding schema

I'm having a collection in my database called businesses. What I want to do is query the database and specific fields, defined in my schema, and not all the fields of the document. I thought that's the reason why the schema exists in the first place…
Andrew
  • 43
  • 1
  • 3
4
votes
3 answers

Find by _id on an array of objects in mongodb database

I'm trying to find the id of an object inside an array of objects. That _id has the same field name _id as others in the document. This is my model (brief) var CardSchema = new mongoose.Schema({ beName: String, beLink: String, cards: [{ …
Julioarhernandez
  • 167
  • 2
  • 11