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

Mongoose: Filter collection based on values in another collection

Consider documents that contain an arrays of ID of another collection, how can I find documents applying a filter based on the related collection using mongoose? I can't find my error Installation.aggregate( …
Gelso77
  • 1,763
  • 6
  • 30
  • 47
6
votes
1 answer

How to find documents where a value is not in an array field in Mongoose?

In Mongoose, if a model M has this field: list: {type:[String]} How should I find a document in which a specific value x is not an element of 'list'? I am hoping there is a special operator '$ncontains' so that I can do the…
Chong Lip Phang
  • 8,755
  • 5
  • 65
  • 100
6
votes
1 answer

Mongoose: How can I access a populated field from a virtual getter?

I have a couple Schemas: var parentSchema = new Schema({name: String}); var childSchema = new Schema({ name: String, parent: {type: Schema.ObjectId, ref: 'Parent'} }); I want to be able to call Child.find().populate('parent') and then use…
6
votes
1 answer

Push element into nested array mongoose nodejs

I am trying to push a new element into an array, I use mongoose on my express/nodejs based api. Here is the code for mongoose: Serie.updateOne({'seasons.episodes.videos._id': data._id}, {$push: {'seasons.episodes.videos.$.reports': data.details}}, …
6
votes
0 answers

MongoDB/ Mongoose many to many relationship - user, project and role

Our project has following requirements- User can be part of multiple projects A project can have multiple users. User can be either Owner or Admin or Viewer of a project. Obviously there can only one Owner of a project. There can be multiple Admin…
6
votes
1 answer

Mongoose compound index creation fields order

I have a question regarding compound index creation in mongodb: Say I want to create this compound index: cSchema.index({account:1, authorization: 1, c_type: 1}); Problem is, javascript doesn't guarantee dictionary order, so I won't be sure the…
6
votes
0 answers

populate mongoose key as part of object

EDIT minimal reproduction repo It's easier to explain in code than English. The following code works, but it feels like there's gotta be an easier, more MongoDBy/mongoosy way ... // recipeModel.js, relevant part of the schema equipments: [{ _id:…
6
votes
1 answer

Error calling 'paginate' after import schema within plugin

I have a problem using paginate in my controller after add plugin to my schema, my code is written in TypeScript 2.1, i have installed @types/mongoose-paginate in devdependencies. [ts] severity: 'Error' message: 'Property 'paginate' does not exist…
6
votes
2 answers

login with email or username with MongoDB in Node.js

I am wondering if there is a way to let user to login with both username or email I searched a lot of times for but doesn't found a working method. I don't have a single idea how to do this, please help with the easiest way if possible. here is what…
Zub
  • 808
  • 3
  • 12
  • 23
6
votes
1 answer

Mongoose TypeError: User is not a constructor

I'm trying to add a subdocument to a parent schema with Mongoose and MongoDB however I'm being thrown the following error: TypeError: User is not a constructor This is based off Mongoose's documentation on subdocuments and I think everything is the…
Rhys Edwards
  • 771
  • 2
  • 14
  • 32
6
votes
1 answer

aggregate returns empty array - mongoose

I have the following two simple queries: Comment.aggregate([{$match: { _id: req.params.id }}]) .exec(function(err, result) { // result is empty }); Comment.find({ _id: req.params.id }) .exec(function (err, result) { // correct…
Stefan
  • 1,041
  • 1
  • 14
  • 28
6
votes
3 answers

How to restrict mongoose default value whose not present in database during find()

I have the following schema. var UserSchema = new mongoose.Schema({ username: { type: String, unique: true, required: true }, password: { type: String, required: true }, test: { type: String, default: 'hello…
waqas jamil
  • 423
  • 3
  • 9
6
votes
1 answer

Mongoose populating path with multiple subpaths

Imagine i have the following models: # MODEL A schemaA = mongoose.Schema _bId: type: mongoose.Schema.Types.ObjectId ref: "B" # MODEL B schemaB = mongoose.Schema _cId: type: mongoose.Schema.Types.ObjectId ref:…
Gbeschbacher
  • 428
  • 8
  • 18
6
votes
2 answers

Do Mongoose defaults get applied to existing documents?

If I add a property to a Mongoose schema and give it a default value, will existing documents receive these defaults when they are loaded?
Tamlyn
  • 22,122
  • 12
  • 111
  • 127
5
votes
1 answer

Mongoose reference in Collection for another Collection

I'll start with this that I'm new to backend and I was looking for some solutions for my problem but i don't know which solution will be right for my problem. So to the point. I'm creating a pizza restaurant project in Next.js with Mongoose and…