mongoose

Getting Timeout error while connecting MongoDB Atlas with Mongoose

不羁岁月 提交于 2021-02-17 05:38:42
问题 I am trying to connect to my database on MongoDB Atlas using mongoose. But every time it's giving me the following error: (node:2327) UnhandledPromiseRejectionWarning: Error: queryTxt ETIMEOUT cluster0-abjwg.gcp.mongodb.net at QueryReqWrap.onresolve [as oncomplete] (dns.js:206:19) (node:2327) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled

using .save() vs findByIdAndUpdate() for removing item from array

你离开我真会死。 提交于 2021-02-16 19:59:41
问题 I am using .pull to remove a record from an array in mongo db and it works fine, but a comment I read somewhere on stack overflow (can't find it again to post the link) is bothering me in that it commented that it was bad to use .save instead of using .findByIdAndUpdate or .updateOne I just wanted to find out if this is accurate or subjective. This is how I am doing it currently. I check if the product with that id actually exists, and if so I pull that record from the array. exports

How to populate nested entities in mongoose?

三世轮回 提交于 2021-02-15 11:03:36
问题 I have the following mongoose schema structure userSchema = new Schema({ roles: [ role: {type: Schema.Types.ObjectId, ref: 'Role' } ] }) rolesSchema = new Schema({ name: String, roleEntities: [ { entity : {type: Schema.Types.ObjectId, ref: 'RoleEntity' }, abilities : [{type: Schema.Types.ObjectId, ref: 'Ability' }] } ] } roleEntitiesSchema = new Schema({ name: String }) abilitiesSchema = new Schema({ name: String }) How can i populate all these nested documents while doing a find on the USER

How to populate nested entities in mongoose?

喜欢而已 提交于 2021-02-15 11:02:55
问题 I have the following mongoose schema structure userSchema = new Schema({ roles: [ role: {type: Schema.Types.ObjectId, ref: 'Role' } ] }) rolesSchema = new Schema({ name: String, roleEntities: [ { entity : {type: Schema.Types.ObjectId, ref: 'RoleEntity' }, abilities : [{type: Schema.Types.ObjectId, ref: 'Ability' }] } ] } roleEntitiesSchema = new Schema({ name: String }) abilitiesSchema = new Schema({ name: String }) How can i populate all these nested documents while doing a find on the USER

翻译| 《 JavaScript无处不在》第5章 数据库(^_^)

只愿长相守 提交于 2021-02-11 21:17:50
翻译| 《 JavaScript无处不在》第5章数据库(^_^) 写在最前面 大家好呀,我是毛小悠,是一位前端开发工程师。正在翻译一本英文技术书籍。 为了提高大家的阅读体验,对语句的结构和内容略有调整。如果发现本文中有存在瑕疵的地方,或者你有任何意见或者建议,可以在评论区留言,或者加我的微信: code _ maomao ,欢迎相互沟通交流学习。 (σ゚∀゚)σ ..: *☆哎哟不错哦 翻译 | 《 JavaScript Everywhere 》第 5 章 数据库(^_^) 第5章 数据库 当我还是个孩子时,我痴迷地收集各类运动卡。收集卡片的过程中很大一部分时间是在整理卡片。我将明星球员放在一个盒子里,有一整个盒子专门放篮球巨星迈克尔·乔丹( Michael Jordan )的纸牌,其余的纸牌则按运动分类,或者按团队分类。这种组织方法使我能够安全地存储卡牌,并在任何给定时间内轻松找到我要找的卡。我自我感觉所知甚少,但是像这样的存储系统实际上就等同于数据库。数据库的核心是允许我们存储信息并在以后检索它。 刚开始进行 Web 开发时,我发现数据库令人生畏。我看到很多有关运行数据库和输入晦涩的 SQL 命令的说明,这感觉就像是一个附加的抽象概念,我无法搞定。值得庆幸的是,我最终跨越了障碍,并且不再被 SQL 表联接所吓倒,因此,如果你仍在踯躅不前,我希望你知道你是可以浏览数据库的世界的。

Memory leak when calling too many promises in Nodejs/Request/MongoDB

可紊 提交于 2021-02-11 17:01:54
问题 When I tried to call up to 200,000 POST requests in NodeJS, it display some errors like heap memory leak. In each POST request, I want to insert the resolved data into localhost mongo DB. It's ok to make 2000 requests at one time but it's really difficult to deal with 200,000 requests. I got stuck in this problem and don't know exactly to resolve it. I really need your help or any suggestions. Thank you in advance for your help. const mongoose = require('mongoose'); const request = require(

My $push method in Mongoose does not work ok

孤街醉人 提交于 2021-02-11 15:02:26
问题 I have a problem understanding why $push creates only one element in nested array and shows only the id of that element const mongoose = require('mongoose'); let historySchema = new mongoose.Schema({ time: { type:String } }) //users schema let userSchema = new mongoose.Schema({ name:{ type:String, }, dob:{ type:String, }, email:{ type:String, }, noOfpeopleClimbing:{ type: Number, default:0 }, details:{ type:String, }, status:{ type: Boolean, default: false }, timeIn:{ type: Number, default: 0

Using `mongoose transactions` when saving message in two collections in mongodb

我是研究僧i 提交于 2021-02-11 14:54:36
问题 If the student sends a message to the teacher, the message is saved in the teachers and students collections. The same situation is when a teacher sends a message to the student, they are also save in the two collections teachers and students . How can I apply mongoose transaction in the following code so that the message is saved in two collections.I don' want a situation that the message will be saved in one collection and in the other collection will not be saved due to an error. I am

mongoose populate in array of custom objects

主宰稳场 提交于 2021-02-11 14:42:28
问题 In the user model, I have an array of custom objects followedPlaylists which contains two attributes ( playlist: the id of the playlist, public: to determine whether it is public or not) as shown below const userSchema = new mongoose.Schema({ ..... other attributes followedPlaylists: [{ playlist: { type: mongoose.Schema.ObjectId, ref: 'Playlist', unique: true }, public: Boolean }] }) I want to populate on followedPlaylists.playlist so the response would be something like [{ playlist: * the

How can I delete a file with multer gridfs storage?

好久不见. 提交于 2021-02-11 14:37:49
问题 The following code works as expected to upload and retrieve files: // Mongo URI const mongoURI = 'mongodbconnstring'; // // Create mongo connection const conn = mongoose.createConnection(mongoURI); // // Init gfs let gfs; conn.once('open', () => { gfs = new mongoose.mongo.GridFSBucket(conn.db, { bucketName: 'Uploads' }); }); // Create storage engine const storage = new GridFsStorage({ url: mongoURI, file: (req, file) => { return new Promise((resolve, reject) => { const filename = file