mongoose – add to an existing array

a new language and a new database means that there are new ways to do things. mongodb is a document-oriented database which means that updating your document is a little more complex than your standard SQL update.

suppose you have a document:

1
2
3
4
5
6
7
8
9
10
11
{
    _id: 1,
    items: [
        {
            itemId: 1,
            title: 'ipod nano black',
            price: '42',
            _id: 1001
        }
    ]
}

and you want to add a new item to the items array in your document. after some digging around, it looks like this works:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
var item = {
    title: 'title of the new item',
    price: '4242'
};
 
// find by document id and update
collection.findByIdAndUpdate(
    1,
    {$push: {items: item}},
    {safe: true, upsert: true},
    function(err, model) {
        console.log(err);
    }
);
 
// find by some conditions and update
collection.findOneAndUpdate(
    {_id: req.query.id},
    {$push: {items: item}},
    {safe: true, upsert: true},
    function(err, model) {
        console.log(err);
    }
);

where collection is your instance of the model you are updating. you can either findByIdAndUpdate if you have the document id handy, otherwise you can findOneAndUpdate and use some conditions to find the record that you want to update.

ugh, this is really simple stuff, it should be easy, but the mongoose documentation would really benefit from a few examples.

node.js, express, consolidate, dustjs-linkedin AND dustjs-helpers

i just started playing with node.js and so far it seems pretty intriguing. my first use case for node.js is to make some JSON APIs. trying to figure out what the structure of a node.js project should be has been a pretty frustrating experience, though.

nevermind that node.js is still in its infancy and the core APIs have been changing at an alarming rate.

nevermind that this means that many blogs that you read about node.js examples won’t work anymore because they were for an older version of node.

what is the most frustrating thing in the world is that trying to figure out what best practice is seems impossible to verify because everyone is still trying to figure out what best practice really is.

that being said, i’ve decided to use express as the web framework. it seems pretty popular, lightweight, and relatively straightforward to use.

i wanted to be able to use a javascript template engine and though there are many, many options out there, i’ve decided to use the linked fork of dust.js. the linkedin guys seem to have a pretty good thing going there and have made some improvements to the engine that really help.

a convenience layer is consolidate which will make swapping template engines relatively painless, though in practice, i don’t know how realistic this is because even though you can swap out a template engine, the views probably won’t be syntactically compatible. still, using consolidate did make setting up dustjs relatively straightforward.

that being said, IF you use consolidate and you want to use dustjs-helpers, there’s really no good documentation on how to do it. the best hint i found was actually in the git repo for dustjs.

1
2
3
4
    app.engine('dust', cons.dust);
    cons.dust.helpers = require('dustjs-helpers');
    app.set('view engine', 'dust');
    app.set('views', __dirname + '/views');