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

Filter with multiple values with multiple operators using mongoose query?

I have array of filter options with multiple operators. Here i provided sample datas and structure. user can select more than one filter with any combination. sample JSON structure i have given below. what is the effective way to get datas using…
Rekha
  • 433
  • 7
  • 22
5
votes
1 answer

Mongoose User, Roles & Permissions

I'm pretty new to NoSQL/Mongo/Mongoose and I'm trying to determine the best way of setting up the schemas. Here's what I have: const UserSchema = new mongoose.Schema( { email: { type: String, required: true }, password: { …
ffserv
  • 53
  • 1
  • 4
5
votes
2 answers

How do I populate a newly created mongoose Document?

I have a case where I am checking whether a document already exists and if it does not exists I am creating a new document. I need to populate 2 fields inside the document. My problem is that the .populate method is not supported on the .create…
5
votes
2 answers

UnhandledPromiseRejectionWarning: TypeError: place.toObject is not a function

Here I am trying to fetch Users Created places using userId. Here are User model and places model and in Controller, I have writing logic to fetch places by userId. Unfortunately, I am getting error "UnhandledPromiseRejectionWarning: TypeError:…
Ferin Patel
  • 3,424
  • 2
  • 17
  • 49
5
votes
3 answers

Type 'Document' is missing the following properties from type

So I have a Node /w Typescript REST API, I have signup method which creates a user and responses with the created users firstName, lastName, email. The problem is I am having this typescript error that says "Type 'Document' is missing the following…
frost kazuma
  • 350
  • 1
  • 8
  • 24
5
votes
2 answers

Mongoose and array of ref UUID's does not convert

When using the library mongoose-uuid, I am able to setup UUID types for my schemas, so when I read the data it is in string (utf-8) format and when I save the data it is in UUID ObjectID BSON type 4 format. This works great with top level or flat…
thxmike
  • 614
  • 1
  • 7
  • 25
5
votes
1 answer

mongodb query nested array with date field

this is my document . "calendar": { "_id": "5cd26a886458720f7a66a3b8", "hotel": "5cd02fe495be1a4f48150447", "calendar": [ { "_id": "5cd26a886458720f7a66a413", "date":…
Babak Abadkheir
  • 2,222
  • 1
  • 19
  • 46
5
votes
4 answers

how to declare mongoose static methods that will work with ESLint

Using Mongoose ORM for MongoDB I've declared a mongoose static method like: ConvoDataSchema.statics.randomItem = async function () { ... } and then create a model with that const ConvoData = mongoose.model('ConvoData', ConvoDataSchema) but later…
dcsan
  • 11,333
  • 15
  • 77
  • 118
5
votes
3 answers

Mongoose Querying Views

I'm currently using mongoose v. 5.25, against mongoDB v.3.6. My application is supposed to query data from many different views, for instance, a view I currently have at my DB: db.joboffers_view.find() will return many records that have been…
5
votes
4 answers

Mongoose array required in schema validation not working

In my schema I need to have a propery that is an array that must be always not null and not undefined. So I defined it required, but the validation is not working as I expected, because if I omit the property no error is throw. In case of simple…
Marco24690
  • 305
  • 1
  • 4
  • 23
5
votes
2 answers

Define variant type in Mongoose

I'm familiar with variant type of OCaml. For example, type foo = | Int of int | Pair of int * string I know how to define Number and String in MongoDB, but my question is how to define the variant type as above in MongoDB: var PostSchema =…
SoftTimur
  • 5,630
  • 38
  • 140
  • 292
5
votes
3 answers

Issue with mongoose .findByIdAndUpdate and pre-update hook

I have mongoose-schema called UserSchema, which stores information about all users. I want to enable the user to change his information, which I try by using .findByIdAndUpdate. This is the relevant code: router.post("/updateprofile",…
A.S.J
  • 627
  • 3
  • 14
  • 38
5
votes
2 answers

mongoose - How to get an object instead of an object reference in a getter?

I create an API with mongoose in Node.js. I saved my data in a collection Transactions which gives some references from other collections objects: const mongoose = require('mongoose'); const { Schema } = mongoose; const transactionSchema = new…
cusmar
  • 1,903
  • 1
  • 20
  • 39
5
votes
0 answers

Mongoose bulk update multiple docs with different values – use timestamps and discriminator

I have a schema that has both timestamps and discriminator key options set. Using Model.save(), everything works fine, but using Model.bulkWrite neither my discriminator key nor the timestamps get saved/updated. Is there a way in mongoose to bulk…
florian norbert bepunkt
  • 2,099
  • 1
  • 21
  • 32
5
votes
0 answers

Mongoose bcryptjs compare password doesn't refer to the document (this)

I have mongoose schema like so var mongoose = require ('mongoose'); var bcrypt = require('bcryptjs'); var Schema = mongoose.Schema; var SALT_WORK_FACTOR = 10; var touristSchema = new Schema ({ local: { email: String, password:…
Ammar Bayg
  • 105
  • 10