mongoose

$lookup when foreignField is array

爷,独闯天下 提交于 2021-02-10 14:41:03
问题 I have 2 collections, the first storing the animes watched by each user, their status etc: const listSchema = mongoose.Schema({ user: { type: Schema.Types.ObjectId, ref: 'user' }, animes: [{ _id: false, anime: { type: Schema.Types.ObjectId, ref: 'anime', unique: true }, status: String, episodes: Number, rating: Number }] }); and the second storing all animes and the relevant information. I want to show that second collection but adding status and episodes fields that are filled from the list

$lookup when foreignField is array

守給你的承諾、 提交于 2021-02-10 14:40:49
问题 I have 2 collections, the first storing the animes watched by each user, their status etc: const listSchema = mongoose.Schema({ user: { type: Schema.Types.ObjectId, ref: 'user' }, animes: [{ _id: false, anime: { type: Schema.Types.ObjectId, ref: 'anime', unique: true }, status: String, episodes: Number, rating: Number }] }); and the second storing all animes and the relevant information. I want to show that second collection but adding status and episodes fields that are filled from the list

Need to push data in nested subdocument array

心不动则不痛 提交于 2021-02-10 14:37:53
问题 I need to push data in nested subdocument array(replyComment): This is an example of a document from my database: { comments: [ { replyComment: [], _id: 601a673735644c83e0aa1be3, username: 'xyz123@gmail.com', email: 'xyz213@gmail.com', comment: 'test123' }, { replyComment: [], _id: 601a6c94d1653c618c75ceae, username: 'xyz123@gmail.com', email: 'xyz123@gmail.com', comment: 'reply test' } ], _id: 601a3b8038b13e70405cf9ea, title: 'latest test', snippet: 'latest test snippet', body: 'latest test

Error in Promise Not sending any error in response object in NodeJS/Express/Mongoose

让人想犯罪 __ 提交于 2021-02-10 14:24:09
问题 I am trying to convert old mongoose promise code to Native ES6 Promise. I am getting all the errors thrown in catch and I can log it in console but when I try to pass it to response object I get empty object. Following is my code module.exports.login = function(req, res) { var userPromise = User.findOne({email:req.body.email}).exec(); userPromise.then(function(user) { if(!user){ throw new Error("step 1 failed!"); } }) .then(function(user) { if(!user.comparePassword(req.body.password)){ throw

Error in Promise Not sending any error in response object in NodeJS/Express/Mongoose

江枫思渺然 提交于 2021-02-10 14:23:40
问题 I am trying to convert old mongoose promise code to Native ES6 Promise. I am getting all the errors thrown in catch and I can log it in console but when I try to pass it to response object I get empty object. Following is my code module.exports.login = function(req, res) { var userPromise = User.findOne({email:req.body.email}).exec(); userPromise.then(function(user) { if(!user){ throw new Error("step 1 failed!"); } }) .then(function(user) { if(!user.comparePassword(req.body.password)){ throw

MongoDB: Check if value is null or the array is empty

痞子三分冷 提交于 2021-02-10 13:09:29
问题 I would like to match all documents that don't contain the "Company" attribute or where the "Company" value is null or an empty array. User.find({Company: {$in: [null, [] ]}}, function (err, users) { if (err) { throw err; } console.log(users.length); }).then(function(doc) { console.log("finish User Company"); }); 回答1: You could use the $or query operator with a check for each condition: { $or: [{ // Check about no Company key Company: { $exists: false } }, { // Check for null Company: null },

Getting a CastError NaN for a Number

痞子三分冷 提交于 2021-02-10 12:27:09
问题 I get this error { [CastError: Cast to Number failed for value "NaN" at path "area"] message: 'Cast to Number failed for value "NaN" at path "area"', name: 'CastError', kind: 'Number', value: NaN, path: 'area' } } } for this code MySchema.methods = { updateArea: function (oldArea, newArea) { var self = this; self.area = self.area - oldArea + newArea; return self.save(function (err) { if (err) { console.log(err); return err; } }); } }; However, when I logged the typeof self.area, it gives me

Two Way Embedding vs. One Way Embedding in MongoDB (Many-To-Many)

守給你的承諾、 提交于 2021-02-10 12:13:27
问题 I would like to discuss with you, if it makes any sense to use two way embedding instead of one way embedding, when modeling an N:M relationship in MongoDB. Let's say, we have two entities: A Product can belong to many (few) Categories , and a Category can have many (lots of) Products . Two Way Embedding If we use this approach, our categories would look like this: { _id: 1, name: "Baby", products: [2] } { _id: 2, name: "Electronics", products: [1, 2] } And products : { _id: 1, name: "HDMI

mongoose: update element in json array

送分小仙女□ 提交于 2021-02-10 09:08:29
问题 I'm trying to update a user document that looks like this: { _id:"xyzdon'tcare", username:"john", examTrials:Array[ { trialId:"x1", examId:"y", questions:Array[ {ques1 Object}, {ques2 object} ] }, { trialId:"x2", examId:"z", questions:Array[ {ques1 Object}, {ques2 object} ] }] } I tried the most straightforward way, findOne() then modify the document then save().The problem is, I faced race condition problems with the document versions when a new request arrived before the previous update

Mongoose query for documents from last 24 hours, only one document per hour

风流意气都作罢 提交于 2021-02-10 05:34:08
问题 I am coding an app in which there are some weather sensors that send air's temperature and humidity to the server every 5 minutes. I would like to draw a chart of how the temperature and humidity change i.e overnight. My idea is to draw the chart basing on data from last 24 hours. I cannot figure it out by myself, so I thought maybe I could seek help here. Of course, each measurement document has a field called createdAt which has the timestamp of the moment it has been created. I need to