Skip to content

Instantly share code, notes, and snippets.

@iorionda
Created February 1, 2016 07:20
Show Gist options
  • Save iorionda/1548460953138dae0ef8 to your computer and use it in GitHub Desktop.
Save iorionda/1548460953138dae0ef8 to your computer and use it in GitHub Desktop.
mongoose の ref と populate の sample
#!/usr/bin/env node
var mongoose = require('bluebird').promisifyAll(require('mongoose'));
var uriUtil = require('mongodb-uri');
var mongodbUri = process.env.MONGOLAB_URI || 'mongodb://localhost/emt-dev';
var mongooseUri = uriUtil.formatMongoose(mongodbUri);
import Company from '../server/api/company/company.model'
import News from '../server/api/news/news.model';
import _ from 'lodash';
mongoose.connect(mongooseUri);
var news = new News({
title: "News Title",
url: 'http://example.com"',
pubDate: "Tue, 05 Jan 2016 21:13:48 -0800"
});
news.save(function(error) {
console.log(news);
Company.findOne({name: 'Company Name'}).exec(function(error, company) {
if(error) throw new Error(error);
company.news.push(news);
company.save(function(error) {
console.log(company);
Company.find({})
.populate('news')
.exec(function(error, companies) {
console.log(JSON.stringify(companies, null, "\t"));
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment