Screen%20region%202014-09-18%20at%2017.37.07_large

Avishaan Sethi

Bluebird with Mongoose

Quick examples of using bluebird with mongoose in node. I just want to show you a couple common use cases to help you recognize how to use promises.

We are going to take common Mongoose use cases for finding and creating documents and going to use Bluebird (or more generally, any promise library that follows Promises/A+) to migrate from the typical callback method to promises.

There are many sites that show you simple examples of promises but sometimes you just:

  • . . .

Read More

November 07, 2015

Use 'type' as a Property Name in a Mongoose Doc

The syntax is a little different since 'type' already has a meaning in a Mongoose schema.

This will not work.

var contractSchema = new mongoose.Schema({
 questions: [{
 type: {type: 'string'},
 question: 'string'
 }],
 version: String,
 state: String
});

This will work.

var contractSchema = new mongoose.Schema({
 questions: [{
 type: 'string'
  . . .

Read More

Posted in: mongoosejsnodejs

October 19, 2014

Mongoose v3 Embedded Object without Explicit Subschema Delcaration

A quick code example of how to embed a subdocument/collection of objects in a Mongoose model without create a whole other explicit subdoc reference.

Jump in!

var contractSchema = new mongoose.Schema({
 questions: [{
 type: {type: 'string'},
 question: 'string',
 choices: ['string'],
 cycles: 'string'
 }],
 version: String,
 state: String
});

This will allow us to have a document in the following form.

var . . .

Read More

Posted in: mongoosejsnodejs

October 19, 2014

Comparing Mongoose Object Ids

Forget about finding a deeply nested id property that you have to call the .toString() method on in order to compare.

Forget about nonsense like this in order to compare two Mongoose Ids

if (val.user.toString() === submissionDoc.owner._id.toString()){
 //do stuff
}

Use a built in Mongoose function instead!

if (val.user.equals(submissionDoc.owner._id)){
 //do stuff
}

Or don't, be convoluted, see if anyone wants to . . .

Read More

Posted in: mongoosejsnodejs

Setember 24, 2014

Archive