mongoose-schema

mongoose TypeError: Schema is not a constructor

拜拜、爱过 提交于 2019-12-21 03:32:20
问题 I've encountered a strange thing. I have several mongoose models - and in one of them (only in one!) I get this error: TypeError: Schema is not a constructor I find it very strange as I have several working schemas. I tried logging mongoose.Schema in the non-working schema and it is indeed different from the mongoose.Schema in my working schemas - how is that possible? The code is almost identical. Here's the code for the non-working schema: var mongoose = require('mongoose'); var Schema =

MongoDB: Checking if nested array contains sub-array

可紊 提交于 2019-12-20 06:32:11
问题 I have a use case where the database is modelled like this: name: XYZ gradeCards: [{ id: 1234, // id of the report card comments: ['GOOD','NICE','WOW'] }, { id: 2345, comments: ['GOOD','NICE TRY'] }] Now, I have a query that I would like to query the schema as follows: I would be given a list of ids and values. For example: the given list is as follows: [{ id: 1234, comments: ['GOOD','NICE'] },{ id: 2345, comments: ['GOOD'] }] In short the ID should be matching and the comments should be a

populate nested array of nested ref

浪子不回头ぞ 提交于 2019-12-20 06:29:14
问题 The structure of the project is structure The main file is index.js that is located in the main directory. This one has 'use strict' var mongoose = require('mongoose'); var app = require('./app'); var port = process.env.port || 3000; mongoose.connect('mongodb://localhost:27017/changeProducts',(err,res) =>{ if(err){ throw err; }else{ console.log("Base de datos funcionando correctamente.."); app.listen(port, function(){ console.log('Servidor nodejs corriendo(app.listen)... '); }); } }); The

How to drop index using Mongoose

孤人 提交于 2019-12-18 07:17:25
问题 Mongoose Imagdata.dropIndexes( { "image_id": 1 }, function(err){ if(err){ res.send("error"); } else{ res.send("success"); } }); This is the code I used for remove the index of a field 'image_id'. When I trying to execute this function It shows "TypeError: Imagdata.dropIndexes is not a function". How to fix this issue... 回答1: You need to use the raw collection property of your model to access the underlying MongoDB API: Imagdata.collection.dropIndex(...); 来源: https://stackoverflow.com

Does Mongoose upsert operation update/renew default schema values?

耗尽温柔 提交于 2019-12-17 20:44:40
问题 Mongoose Schema: new Schema({ ... createDate: { type: Date, default: Date.now }, updateDate: { type: Date, default: Date.now } }); Upsert operation: const upsertDoc = { ... } Model.update({ key: 123 }, upsertDoc, { upsert: true }) when I upsert with update or findOneAndUpdate the default schema values createDate and updateDate are always renewed no matter document is inserted or updated. It's same when I use $set (in which of course I don't pass dates). I don't seem to find anything to tell

Mongoose TypeError: User is not a constructor

跟風遠走 提交于 2019-12-17 20:37:18
问题 I'm trying to add a subdocument to a parent schema with Mongoose and MongoDB however I'm being thrown the following error: TypeError: User is not a constructor This is based off Mongoose's documentation on subdocuments and I think everything is the same. How can I debug this further? Router // Add a destination to the DB router.post('/add', function(req, res, next) { let airport = req.body.destination let month = req.body.month let id = (req.user.id) User.findById(id , function (err, User) {

Pushing item to Mongodb collection array

风流意气都作罢 提交于 2019-12-13 21:41:13
问题 I am trying to push a value to mongodb collection arrayList (user.completed) but it's not getting updated even though my code seems correct. When I make the API call, the values are being passed however "$push" isn't adding value to completed array!! What am I missing here? When I try to push to value to a string, I get mongoose error as expected. But when I try to push value to an undefined key ('abc'), a new array doesn't get created like the mongodb documentation suggest! Not sure what is

How to update existing user data in a node.js and MongoDB app with ejs post form?

不问归期 提交于 2019-12-13 04:32:41
问题 I am extremely new to javascript and found Brad Traversy's video Node.js with Passport Authentication and followed his video to a t. It worked for me, but then I wanted to add more. I created a custom dashboard and navigation. One page I added was a user profile page. I added this because I want to be able to update the user's information if possible. Now I'm stuck. I think I'm having an issue with my .findOneAndUpdate function that I placed in my route index.js. I may not be using the

Virtual field not setting field in mongoose model

假装没事ソ 提交于 2019-12-13 04:06:24
问题 I am new to nodeJS and mongoose. I am trying to make a user model that does not save a password as plain text. In other backend frameworks you can accomplish this with an ORM by utilizing a virtual field. I looked up the docs for Mongoose and found that this can be accomplished. Following the dics I created the following Mongoose model. Mind you this is not the final implementation and is for merely testing my understanding of how Mongoose handle virtual fields. const mongoose = require(

How to project updated values only using findOneAndUpdate in embedded array Mongoose?

回眸只為那壹抹淺笑 提交于 2019-12-13 03:49:28
问题 Currently my User model looks like: { _id: 'SomeId' firstName: 'John', lastName: 'Cena', books: [ { _id: 'xyz', title: 'a', author:'b', ratings:[ {source:'source1', value:"8"}, {source:'source2', value:"9"}] }, { _id: 'abc', title: 'c', author:'d', ratings:[ {source:'source3', value:"7"}, {source:'source4', value:"5"}] } ] } After making an findOneAndUpdate query to update rating=>value of 1st book object(_id: "xyz") from 8 to 10 for a given source(say "source1") : let up={ 'books.$[book]