使用 mongodb 嵌入集合
Embed the collections using mongodb
我刚开始使用 mongodb 和 nodejs。我知道 mongodb 不支持 joins.I 只需将数据插入 mongodb 并且我的文档看起来:
{
"_id": ObjectId("564dacf84d52785c1d8b4567"),
"content": "This blog created by karanSofat",
"html": "<p>This blog created by karanSofat</p>\n",
}
现在用户对此发表评论 post。应该是这样的:
{
"_id": ObjectId("564dacf84d52785c1d8b4567"),
"comments": [
{
"name": "sumit",
"email": "sumit@ggi.net",
"comment": "this is also well for me",
"posted_at": ISODate("2015-11-19T11:06:27.172Z")
}
],
"content"▼: "This blog created by karanSofat",
"html": "<p>This blog created by karanSofat</p>\n",
}
这是我的模型,
//post model
// grab the mongoose module
var mongoose = require('mongoose');
// define our nerd model
// module.exports allows us to pass this to other files when it is called
module.exports = mongoose.model('post', {
content : {type : String, default: ''},
html : {type : String, default: ''}
});
//comment model
var mongoose = require('mongoos
module.exports = mongoose.model('comment', {
name : {type : String, default: ''},
email : {type : String, default: ''},
comment : {type : String, default: ''},
posted_at : {type : date, default: ''}
});
我的问题是我不知道使用 nodejs 以何种方式插入评论数据并且我的文档将嵌入。
这是我的代码:
app.post('/comments/:id', function(req, res) {
var Comment = require("../app/models/comments");//comment Model
var blog = require("../app/models/blog");//blog model
var id = req.params.id; //postId
var comments = JSON.parse(JSON.stringify(req.body)); //commentdata
//code Should be here
res.json({data:id,data2:input});
});
请帮忙
卡兰
假设您有以下架构:
var Comments = new Schema({
name: String,
email: String,
comment: String,
, posted_at: Date
});
var BlogPost = new Schema({
content : String,
html : String,
comments : [Comments],
});
mongoose.model('BlogPost', BlogPost);
您可以像这样将嵌入文档添加到数组中:
// retrieve my model
var BlogPost = mongoose.model('BlogPost');
// create a blog post
var post = new BlogPost();
// create a comment
post.comments.push({
"name": "sumit",
"email": "sumit@ggi.net",
"comment": "this is also well for me",
"posted_at": ISODate("2015-11-19T11:06:27.172Z")
});
post.save(function (err) {
if (!err) console.log('Success!');
});
我刚开始使用 mongodb 和 nodejs。我知道 mongodb 不支持 joins.I 只需将数据插入 mongodb 并且我的文档看起来:
{
"_id": ObjectId("564dacf84d52785c1d8b4567"),
"content": "This blog created by karanSofat",
"html": "<p>This blog created by karanSofat</p>\n",
}
现在用户对此发表评论 post。应该是这样的:
{
"_id": ObjectId("564dacf84d52785c1d8b4567"),
"comments": [
{
"name": "sumit",
"email": "sumit@ggi.net",
"comment": "this is also well for me",
"posted_at": ISODate("2015-11-19T11:06:27.172Z")
}
],
"content"▼: "This blog created by karanSofat",
"html": "<p>This blog created by karanSofat</p>\n",
}
这是我的模型,
//post model
// grab the mongoose module
var mongoose = require('mongoose');
// define our nerd model
// module.exports allows us to pass this to other files when it is called
module.exports = mongoose.model('post', {
content : {type : String, default: ''},
html : {type : String, default: ''}
});
//comment model
var mongoose = require('mongoos
module.exports = mongoose.model('comment', {
name : {type : String, default: ''},
email : {type : String, default: ''},
comment : {type : String, default: ''},
posted_at : {type : date, default: ''}
});
我的问题是我不知道使用 nodejs 以何种方式插入评论数据并且我的文档将嵌入。 这是我的代码:
app.post('/comments/:id', function(req, res) {
var Comment = require("../app/models/comments");//comment Model
var blog = require("../app/models/blog");//blog model
var id = req.params.id; //postId
var comments = JSON.parse(JSON.stringify(req.body)); //commentdata
//code Should be here
res.json({data:id,data2:input});
});
请帮忙
卡兰
假设您有以下架构:
var Comments = new Schema({
name: String,
email: String,
comment: String,
, posted_at: Date
});
var BlogPost = new Schema({
content : String,
html : String,
comments : [Comments],
});
mongoose.model('BlogPost', BlogPost);
您可以像这样将嵌入文档添加到数组中:
// retrieve my model
var BlogPost = mongoose.model('BlogPost');
// create a blog post
var post = new BlogPost();
// create a comment
post.comments.push({
"name": "sumit",
"email": "sumit@ggi.net",
"comment": "this is also well for me",
"posted_at": ISODate("2015-11-19T11:06:27.172Z")
});
post.save(function (err) {
if (!err) console.log('Success!');
});