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

Nested objects in mongoose schemas

i've seen many answers to this question here, but i still don't get it (maybe because they use more "complex" examples)... So what im trying to do is a schema for a "Customer", and it will have two fields that will have nested "subfields", and…
Samuel E.
  • 2,320
  • 2
  • 26
  • 31
66
votes
6 answers

Mongoose document references with a one-to-many relationship

I'm working on designing a database structure for a new project, and I'm pretty new to MongoDB, and obviously Mongoose. I've read Mongooses population documentation, where it has a one-to-many relationship, with one Person document to many Story…
Justin
  • 1,959
  • 5
  • 22
  • 40
45
votes
4 answers

Mongoose - remove multiple documents in one function call

In documentation there's deleteMany() method Character.deleteMany({ name: /Stark/, age: { $gte: 18 } }, function (err) {}); I want to remove multiple documents that have one common property and the other property varies. Something like…
Maciej Krawczyk
  • 14,825
  • 5
  • 55
  • 67
33
votes
8 answers

Find one or create with Mongoose

I have Page.findById(pageId).then(page => { const pageId = page.id; .. }); My problem is that if no page id is given, it should just take the first available page given some conditions, which is done by Page.findOne({}).then(page => { const…
Jamgreen
  • 10,329
  • 29
  • 113
  • 224
27
votes
7 answers

NestJS - How to create nested schema with decorators

Let's say I want to build the below schema with mongoose: const userSchema = new Schema({ name: { firstName: String, lastName: String } }) How can I do it with NestJS decorators (@Schema() & @Prop())? I try this method, but no…
Sinandro
  • 2,426
  • 3
  • 21
  • 36
26
votes
4 answers

Should I Use Schema.Types.ObjectId or Schema.ObjectId When Defining a Mongoose Schema

It seems like defining my Schema this way: var PossessionSchema = new mongoose.Schema({ thing: {type: mongoose.Schema.Types.ObjectId, ref:"Thing"} }); or this way: var PossessionSchema = new mongoose.Schema({ thing: {type:…
ek_ny
  • 10,153
  • 6
  • 47
  • 60
25
votes
1 answer

Is the Mongoose timestamps schema option indexed?

Mongoose version >= 4.0 has a timestamps option which creates an updatedAt and createdAt field for schemas when timestamps is set to true. http://mongoosejs.com/docs/guide.html#timestamps Are the updatedAt and createdAt fields indexed?
steampowered
  • 11,809
  • 12
  • 78
  • 98
25
votes
2 answers

How to select specific field in nested populate in mogoose

here is my schema: var UserSchema = new Schema({ email: String, name: String, city: String, username: String, profilePic: String, phoneNo: Number, shortList: { project: [{ type: Schema.ObjectId, ref: "Project"…
Rhushikesh
  • 3,630
  • 8
  • 45
  • 82
23
votes
17 answers

mongoose TypeError: Schema is not a constructor

I've encountered a strange thing. I have several mongoose models - and in one of them (only in one!) I get this error: TypeError: Schema is not a constructor I find it very strange as I have several working schemas. I tried logging mongoose.Schema…
David Stenstrøm
  • 762
  • 1
  • 8
  • 22
19
votes
3 answers

Validate object against Mongoose schema without saving as a new document

I'm trying to validate some data that will be inserted into a new document, but not before a lot of other things need to happen. So I was going to add a function to the static methods that would hopefully validate objects in an array against he…
Justin
  • 1,959
  • 5
  • 22
  • 40
18
votes
4 answers

Mongoose $push keeps adding two entries

Here are my user and product schemas: const productSchema = new Schema({ //... addedBy: { type: mongoose.Schema.Types.ObjectId, ref: "users" } }); const userSchema = new Schema({ //... addedItems: [{ type:…
Phil
  • 3,342
  • 5
  • 28
  • 50
18
votes
3 answers

JSDoc + Mongoose : how to document Mongoose models?

I have Mongoose schema and a model: var MyClientSchema = new mongoose.Schema({ fist_name: { type: String }, phone_number: { type: String } }); var MyClient = mongoose.model('MyClient', MyClientSchema); How should I…
Jan Grz
  • 1,373
  • 14
  • 18
17
votes
5 answers

How to check modified fields in pre/post update hook in Mongoose.js

I need to know modified fields or if a specific field was modified in pre or post update hook in a Mongoose schema. I tried the following, but still unable to figure it out: schema.post('update', function (doc) { //check modified fields or if…
jemlifathi
  • 1,482
  • 5
  • 22
  • 32
16
votes
2 answers

How to find by nested property in mongoose

I'm trying to find an object in my database by a nested property, I can't seem to find any way to do it. My schema is below and I have shown how I've attempted to query. var stations = { Alpha: Number, Beta: Number }; var systemSchema = new…
Satvir Sandhu
  • 181
  • 1
  • 1
  • 7
16
votes
5 answers

Preventing duplicate records in Mongoose

I'm fairly new to MongoDb / Mongoose, more used to SQL Server or Oracle. I have a fairly simple Schema for an event. EventSchema.add({ pkey: { type: String, unique: true }, device: { type: String, required: true }, name: { type: String,…
Code Uniquely
  • 6,356
  • 4
  • 30
  • 40
1
2 3
99 100