Tag: mongodb

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 || […]

Most common distinct values mongodb

How to get the most common distinct values of a key in query results. Consider a collection ‘collectionSample’ { name : ‘a’, value: 10, word : ‘baz’ }, { name : ‘a’, value: 65, word : ‘bar’ }, { name : ‘a’, value: 3, word : ‘foo’ }, { name : ‘b’, value: 110, word […]

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 […]

remove query in mongodb by _id with nodejs

removing a row in mongodb isn’t seem to be that easy as in SQL lol, well, my issue is that I cant remove a row by the _id because the _id contains an obj ObjectId, that’s the row in the db, { “_id” : ObjectId(“541ec60e41b46b841adde31e”), “name” : “TT” } and this is how I’m trying […]

Using Gulp to manage opening and closing MongoDB

So far I have a child_process that executes ‘mongod –dbpath db’ and another child_process which can kill it with ‘mongod –dbpath db –shutdown’. How do I listen for the user to enter ctrl-c or exit the gulp runner, and then run the gulp task to shutdown mongo?

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 = […]

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.