mongoose

Mongoose save() not saving changes

女生的网名这么多〃 提交于 2021-02-11 12:28:02
问题 I have a fully functioning CRUD app that I'm building some additional functionality for. The new functionality allows users to make changes to a list of vendors. They can add new vendors, update them and delete them. The add and delete seem to be working just fine, but updating doesn't seem to be working even though it follows a similar method I use in the existing CRUD functionality elsewhere in the app. Here's my code: // async function from AXIOS request const { original, updatedVendor } =

mongoose model, array of strings, array of objects structure

[亡魂溺海] 提交于 2021-02-11 07:24:43
问题 I am trying to design my database model and i dont have clue how to fit array of strings and array of objects into it. My current model is: const mongoose = require("mongoose"); const Schema = mongoose.Schema; const schema = new Schema({ email: { type: String, unique: true, required: true }, hash: { type: String, required: true }, createdDate: { type: Date, default: Date.now }, settings: { favorites: { /* ??? */ }, cart: { /* ??? */ }, states: { favorites: { type: Boolean, default: true },

find({}) returns an empty array mongoose

依然范特西╮ 提交于 2021-02-11 06:56:10
问题 Here is my code in model.js file: const mongoose = require("mongoose"); const con = require("./connection"); con(); const schoolNotices = mongoose.model("schoolNotices", { title:{ type: String }, date:{ type:String }, details:{ type:String } }); module.exports = schoolNotices; And I have imported this code into students.js file router.route("/notices") .get((req,res)=>{ schoolNotices.find({},(err,docs)=>{ if(err){ return console.log(err) } res.render("studentNotices",{title:"Notices",data

Mongoose join two collections and get only specific fields from the joined collection

人盡茶涼 提交于 2021-02-11 06:55:00
问题 I have a problem joining two collections in mongoose. I have two collections namely: student and exams. Student model: { fullname: { type: String, required: true }, email: { type: String, required: true }, } Exams model: { test: { type: String, required: false }, top10: [ type: { studentId: { type: String, required: true }, score: { type: Number, required: false }, } ] } Now, I want to join them two by studentId . The result should be: { "test": "Sample Test #1", "students": [ { "studentId":

Mongoose join two collections and get only specific fields from the joined collection

人盡茶涼 提交于 2021-02-11 06:54:05
问题 I have a problem joining two collections in mongoose. I have two collections namely: student and exams. Student model: { fullname: { type: String, required: true }, email: { type: String, required: true }, } Exams model: { test: { type: String, required: false }, top10: [ type: { studentId: { type: String, required: true }, score: { type: Number, required: false }, } ] } Now, I want to join them two by studentId . The result should be: { "test": "Sample Test #1", "students": [ { "studentId":

Typescript & Mongoose - “this” not available in instance methods

久未见 提交于 2021-02-11 04:29:32
问题 I am currently converting my API from JS to TS. However, I'm having some difficulties with mongoose & typescript. Specifically, this is not available inside my instance methods. My code: AccountSchema.methods.comparePassword = async function (candidatePassword: string) { const isMatch = await bcrypt.compare(candidatePassword, this.password); return isMatch; }; Which generates the following error as this refers to the function rather than the actual document: Argument of type 'Function' is not

Mongoose FindOneAndUpdate an embedded object and return parent

会有一股神秘感。 提交于 2021-02-11 04:21:56
问题 I'm having a similar problem laid out in this question, but I'm stumbling on a different aspect. I have an array of objects/subdocuments embedded within a document in my mongo database, schema like this.... const projectSchema = new mongoose.Schema({ name: { type: String }, ... stakeholders: [{ stakeholderTitle: { type: String, max: 150, required: [true, 'A stakeholder must have a title.'] }, ... }], what I need to do is update a particular embedded object based on the query, but I still need

Unable to connect to MongoDB Atlas using Mongoose with connection string [closed]

可紊 提交于 2021-02-10 20:18:51
问题 Closed. This question needs debugging details. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 1 year ago . Improve this question These are the errors I received: (node:11692) UnhandledPromiseRejectionWarning: MongoNetworkError: connection 5 to cluster0-shard-00-01-wwvto.mongodb.net:27017 closed at TLSSocket. (C:\Node Example\node-rest-shop\node_modules\mongodb-core\lib\connection\connection.js:276:9) at

Mongoose & Express: How to properly Remove, Create & Store data that are reference

混江龙づ霸主 提交于 2021-02-10 20:10:46
问题 The first problem I am having is that whenever I try to delete the Comment, I also try to find the index of that specific comment inside post.comments as well as inside user.comments , it consistently returns -1, the reason why I am trying to find it, is so that I can splice it from the comments array that user and post do have. The second problem I am having is that whenever I create a comment, I try to store it in the comments array that user and post have, but it stores it only as a string

Mongoose & Express: How to properly Remove, Create & Store data that are reference

心不动则不痛 提交于 2021-02-10 20:10:20
问题 The first problem I am having is that whenever I try to delete the Comment, I also try to find the index of that specific comment inside post.comments as well as inside user.comments , it consistently returns -1, the reason why I am trying to find it, is so that I can splice it from the comments array that user and post do have. The second problem I am having is that whenever I create a comment, I try to store it in the comments array that user and post have, but it stores it only as a string