Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have made a collection post just as a tester which for now includes 2 documents. It has an embedded collection comments where i have entered 2 docs. A comment has a title, content and likes. I would like to know how could I add another field which is a collection named replies(Basically replies to the comments)?
Here is my insert code for the initial documents:

post=db.post<br />
post.insert<br><br />
(<br />
	[<br />
		{title:"My first Post",<br />
		content:"Hello World this is my first post 		at the website!!!",<br />
		time: new Date(),<br />
		by:{<br />
			name: "Mohit Hiralal",<br />
			email:"mh@gmail.com"}<br />
		},<br />
		{title:"My second post",<br />
		content:"This is my second post. Hope this garners some atention!",<br />
		time: new Date(),<br />
		by{<br />
name:"Mohit",email:"m@gmail.com"<br />
		},<br />
		comments:<br />
		[<br />
			{title:"Spelling wrong",<br />
			content:"You spelled attenttion wrong…",<br />
			likes:1009}<br />
		]<br />
		}<br />
]<br />
)
Posted
Updated 5-Aug-15 3:45am
v3

1 solution

Mainly, there are 2 approaches to achieve that goal.


You can embed the replies as a field in the post document. Something like:


JavaScript
post.insert
(
[
	// ...
	comments:
	[
	    {title:"Spelling wrong",
	    content:"You spelled attenttion wrong…",
	    likes:1009,
	    replies: []}
	]
	// ...
]
);

Or, using another collection for replies that holds the key-value of the appropriate comment (like in relational databases).


For more information you can see the MongoDB documentation about: Data Modeling.

 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900