一つのモノゴセの例
1518 ワード
var express = require('express'),
mongoose = require('mongoose'); // mongoose
// mongodb nodejs
mongoose.connect('mongodb://localhost/nodejs');
// Schema ObjectId
var Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
// Schema( & ) mongodb collection( )
var commentsShema = new Schema({
name:String,
content:String
});
// comments collection Model mongodb collection ( )
var CommentModel = mongoose.model('comments', commentsShema);
// Schema
var newsSchema = new Schema({
title : String,
source : String,
content : String,
comments :{type:[commentsShema], default:[]} // , (collection), (type:[commentsShema]), (default:[])
});
// Model
var NewsModel = mongoose.model('news', newsSchema);
//
var news = new NewsModel();
news.title = ' : ';
news.source = 'http://finance.qq.com/a/20120221/001221.htm';
news.content = ' , ....';
news.save(function(err){
console.log(err);
});
//
NewsModel.find({}, function(err,docs){
if(!err){
if(docs[0]){
//
var comment = new CommentModel();
comment.name = 'iblue';
comment.content = ' !';
docs[0].comments.push(comment);
NewsModel.update({_id:docs[0]._id}, {comments:docs[0].comments}, function(err){
console.log(err);
});
}
}
});