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

Unable to connect to Mongodb from Nodejs

I am trying to connect to Mongodb from my first node project.I am using Mongoose to achieve that. However, mongoose.connect() gives me MongoParseError error. Complete error: MongoParseError: options connections, models, events, __driver, options,…
0
votes
2 answers

How can I set the refPath to look on the property that is in the same object

My code simply is I want to let the user bookmark different types of products. This is on the user schema const UserSchema = new mongoose.Schema({ // ... bookmarks: [ { product_type: { // this should be the refPath type:…
0
votes
1 answer

How to allow unique fields in subdocuments when using mongoose?

I'm using mongoose to to define 2 schemas. employee.js const mongoose = require("mongoose"); const uniqueValidator = require("mongoose-unique-validator"); const Role = require("./role"); const employeeSchema = mongoose.Schema({ code: { type:…
Hikaros
  • 101
  • 1
  • 12
0
votes
1 answer

How to populate an array of ObjectIds in mongoose?

I have a User model with a schema that I would like to validate an array of multiple friends by their id's. The portion of the schema that is supposed to do this is: friends: { type: [mongoose.SchemaTypes.ObjectId], }, Then, when I try to add a…
0
votes
0 answers

How to add reference for mongoose of many

I have three schemas - User, Community and User: User: export const UserSchema = new Schema({ address: { type: String, unique: true }, name: String }, { timestamps: true }); const UserModel = model("User",…
Petr
  • 1,853
  • 2
  • 25
  • 49
0
votes
1 answer

How to update and Save Array object in MongoDB (Mongoose + MongoDB)

I want to save the interview array object. I want when scheduled round 1 occurs and their feedback too based on round 1, want to save round 1 data with feedback and round 2 data with feedback, and so on interview array has two objects schedule (for…
0
votes
2 answers

Error: Uploaddb validation failed: file: Path `file` is required

I am trying to upload a form data consisting of name, email, and pdf file and i am currently stuck at uploading part when I submit the form I get this error: html part (form):
0
votes
2 answers

Mongoose leaves creates an empty array

Inserted Object newSale { productCode: 'MRS-GT-EN', amount: 359.89, serial: 4143, orderNumber: 2241, bookingDates: [ { date: '9-1-2023', time: 6 }, { date: '10-1-2023', time: 6 }, { date: '11-1-2023', time: 6 }, { date:…
AlZ
  • 19
  • 7
0
votes
0 answers

How to use Object type in mongose schema?

I am trying to store the amount of time an employee has worked in my MongoDB database, but not able to make a mongoose schema whose type will object. The desired database should have a document like this: { name: 'name of employee', report:…
akratre
  • 21
  • 2
0
votes
1 answer

How to filter documents using find method in mongoose based on the data from reference in documents?

I am working on e-commerce like app. I have orderItem Schema const orderItemsSchema = mongoose.Schema( { order: { type: mongoose.Schema.Types.ObjectId, ref: 'OrderItems', required: true, }, product: { type:…
0
votes
0 answers

Populating field of schema using pre middleware in nodejs

` const mongoose = require('mongoose'); const postSchema = new mongoose.Schema( { content: { type: String, required: true }, user: { type:mongoose.Schema.Types.ObjectId, ref:'User' }, …
0
votes
1 answer

How to populate an ObjectId object within an array in Mongoose?

First of all, I found a lot of answers about that problem in StackOverflow However, none of them worked on my problem. I have an userSchema like that: const UserSchema = new Schema({ userName: { type: String, required: true, }, …
0
votes
1 answer

mongoose, graphQL query returning null data though exist in mongo

I am trying to use a query in NodeJS using mongoose + GraphQL. The model file as: const mongoose = require('mongoose') mongoose.set('debug', true); const Schema = mongoose.Schema const authorSchema = new Schema({ name: String, age:…
u_peerless
  • 644
  • 2
  • 9
  • 23
0
votes
1 answer

Custom Sorting in Mongoose

I have the following schema of story: story : { content: string, seenBy: Types.ObjectId[] } here seenBy is the array of users, Now I have a list of stories and I want to sort the list based on the unseen story first and seen at last for a…
Vats Patel
  • 11
  • 2
0
votes
0 answers

how to store login history in node js using mongoose schema

i am at initial stage of learning mern stack i dont know to how store the login history whenever a user login i tried to access with jwt token which is stored in my localstorage app.post("/apexa_Admin_login", async (req, res) => { //creating login…