Skip to content

Instantly share code, notes, and snippets.

@s-leroux
Created March 20, 2016 17:38
Show Gist options
  • Save s-leroux/0872317c9b81f181a69b to your computer and use it in GitHub Desktop.
Save s-leroux/0872317c9b81f181a69b to your computer and use it in GitHub Desktop.
"Server-side Development with NodeJS" Course -- Mongoose concurrent insert of sub-document test
> db.dishes.create({
... _id: ObjectId("56eedb8b839b02cc2b7954d5"),
... name: 'Uthapizza',
... description: 'Test',
... comments: [
... {
... rating: 3,
... comment: 'This is insane',
... author: 'Matt Daemon'
... }
... ]
... })
var mongoose = require('mongoose'),
assert = require('assert');
var Dishes = require('./models/dishes-3');
// Connection URL
var url = 'mongodb://localhost:27017/conFusion';mongoose.connect(url);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
// we're connected!
console.log("Connected correctly to server");
var id = mongoose.mongo.ObjectId("56eedb8b839b02cc2b7954d5");
// get all the dishes
Dishes.findByIdAndUpdate(id, {
$set: {
description: 'Updated Test'
}
}, {
new: true
})
.exec(function (err, dish) {
setTimeout(function () {
if (err) throw err;
console.log('Updated Dish!');
console.log(dish);
dish.comments.push({
rating: 5,
comment: 'I\'m getting a sinking feeling!',
author: 'Leonardo di Carpaccio'
});
dish.save(function (err, dish) {
console.log('Updated Comments!');
console.log(err);
console.log(dish);
db.close();
});
}, 3000);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment