简体   繁体   English

使用mongodb嵌入Json数据

[英]Embed Json Data using mongodb

I am using mongodb with nodejs. 我将mongodb与nodejs一起使用。 I just starting for using mongodb. 我刚刚开始使用mongodb。 I have one query.Here is my mongdb Document: 我有一个查询,这是我的mongdb文档:

{
   "_id": ObjectId("564dacf84d52785c1d8b4567"),
    "content": "This blog created by karanSofat",
   "html": "<p>This blog created by karanSofat</p>\n",
   "saved_at": ISODate("2015-11-19T11:05:28.618Z"),
   "title": "my blog" 
}

I want to make relation with this data. 我想与此数据建立联系。 I want my json should like when user comment. 我希望我的json在用户评论时会喜欢。

{
   "_id": ObjectId("564dacf84d52785c1d8b4567"),
   "comments": [
     {
       "name": "arpit",
       "email": "arpit@gmail.com",
       "comment": "How can Make we this at good",
       "posted_at": ISODate("2015-11-19T11:06:03.628Z") 
    },
     {
       "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",
   "saved_at": ISODate("2015-11-19T11:05:28.618Z"),
   "title": "my blog" 
}

I am using angular js and node js.Here is my code: 我正在使用angular js和node js。这是我的代码:

app.post('/comments/:id', function(req, res) {
var id = req.params.id;
var comments = JSON.parse(JSON.stringify(req.body));



res.json({data:id,data2:input});
});

Make a new mongoose model called comment with the structure: 使用以下结构创建一个名为comment的新猫鼬模型:

{
    name: {
        type: String
    },
    email: {
        type: String
    },
    comment: {
        type: String
    },
    posted_at: {
        type: Date
    }
}

Each doc will get assigned it's own unique _id. 每个文档都会被分配一个自己的唯一_id。 When you save each comment, update the comments array on your blog_post documents. 保存每个评论时,请更新blog_post文档上的评论数组。 You can then utilize Mongoose's builtin 'populate' functionality: 然后,您可以利用Mongoose的内置“填充”功能:

http://mongoosejs.com/docs/populate.html http://mongoosejs.com/docs/populate.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM