Tag: mongoose

Mongodb multi document insert ignore custom duplicate field error

I have to insert 3 recordset from array 1 already exists and 2 are new e.g: db.products.insert( [ { imagename: “pen1”, qty: 21 }, { imagename: “pen”, qty: 20 }, { imagename: “eraser”, qty: 25 } ] ) Wherein “{ imagename: “pen”, qty: 20 }”` already exists and has unique key on field “imagename” in […]

Mongoose duplicate key error with upsert

I have problem with duplicate key. Long time can`t find answer. Please help me solve this problem or explain why i get duplicate key error. Trace: { [MongoError: E11000 duplicate key error collection: project.monitor index: _id_ dup key: { : 24392490 }] name: ‘MongoError’, message: ‘E11000 duplicate key error collection: project.monitor index: _id_ dup key: […]

Node.js + Mongoose works locally, but not on Heroku

Here’s my node.js file I’m running on Heroku. It works perfectly locally, but when I push it to Heroku, the database collection isn’t created and no errors are thrown. Ultimately I’m just trying to get it to actually creating the connection while it’s running on Heroku. Thanks. var mongoose = require(‘mongoose’), db_url = process.env.MONGOHQ_URL || […]

Mongoose – sub document validation not working

I have a Schema that looks like so: var minderSchema = mongoose.Schema({ phones: {type: [{ details: { type: { country_code: { type: String, required: true }, region_code: { type: String, required: true }, number: { type: Number, required: true } }, required: true }, }], required: true}, }) That is… a minder is made up […]

Mongoose – REST API – Schema With Query to different model

I’m trying to avoid DB Callback Queries. Assuming that you have two schemas that looks like so : 1st) User Schema username : {type: String, unique: true}, age : {type: Number} 2nd) Activity Schema owner: [{type: Schema.Types.ObjectId, ref: ‘User’}], city: {type: String}, date: {type: Date} So far so good. Now lets say you have a […]

Error: Can't set headers after they are sent , happen only after refreshing a page

Every time i refresh a html page at route ‘localhost:8080/’ or login a user I will get this Error: Can’t set headers after they are sent. As far as I know this problem happens due to callbacks that are accidentally called twice. The app doesn’t crash or anything, its just keep logging the same error […]

Need to send response after forEach is done

I’m working with NodeJS + Mongoose and I’m trying to populate an array of objects and then send it to the client, but I can’t do it, response is always empty because it is sent before forEach ends. router.get(‘/’, isAuthenticated, function(req, res) { Order.find({ seller: req.session.passport.user }, function(err, orders) { //handle error var response = […]

Having problems exporting model functions (Express and Mongoose)

I have been looking at code (https://github.com/cmarin/MongoDB-Node-Express-Blog) to learn NodeJS, Express, Mongoose, and I am having trouble importing a ‘Poll’ function from my ‘models.js’ file, particularly the ‘save’ function. I am getting the following error: 500 TypeError: Object function (){} has no method ‘save’ It occurs on line 54 of my app.js. I am unable […]

How do I create an automatic field in Mongoose?

I have a model that looks like: var CompanySchema = new Schema({ name: String , logoUrl: String , created: { type: Date, default: new Date().toUTCString() } , deleted: { type: Date, default: null } }); I want to have a field called id as well (this is on top of the _id that already gets […]

mongoose.js: _id of embedded document

I am trying to save a task to a list of tasks with mongoose and MongoDB. I want to save it redundantly in the tasks collection and in the corresponding list document as embedded document. It works fine but one little thing: The list’s embedded documents don’t have their objectIds. But I need them in […]

Node.js is the Best Javascript runtime in the world.