Guest User

Untitled

a guest
Apr 25th, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. var CommentSchema = mongoose.Schema({
  2. body: String,
  3. created: {
  4. by: {
  5. type: String,
  6. required: true
  7. },
  8. date: {
  9. type: Date,
  10. default: Date.now
  11. }
  12. }
  13. });
  14.  
  15. var BlogSchema = mongoose.Schema({
  16. title: String,
  17. blog: String,
  18. created: {
  19. by: {
  20. type: String,
  21. required: true
  22. },
  23. date: {
  24. type: Date,
  25. default: Date.now
  26. }
  27. },
  28. comments: [CommentSchema]
  29. });
  30.  
  31. var Blog = mongoose.model('Blog', BlogSchema);
  32.  
  33. Blog.findOne({
  34. _id: id
  35. }, function(err, blog) {
  36. console.log(blog); // causes toJSON to be executed
  37. });
  38. // outputs:
  39. {
  40. title: 'My first blog! #Super',
  41. blog: 'This is my very first #blog! I hope you enjoy it. #WOOHOO',
  42. _id: 532cb63e25e4ad524ba17102,
  43. __v: 0,
  44. comments: [], // SHOULD THIS BE INCLUDED??
  45. created: {
  46. by: 'Joe',
  47. date: Fri Mar 21 2014 17: 59: 26 GMT - 0400(EDT)
  48. }
  49. }
  50.  
  51. Blog.findOne({
  52. _id: id
  53. }, 'title', function(err, blog) {
  54. console.log(blog); // causes toJSON to be executed
  55. });
  56. // outputs:
  57. {
  58. title: 'My first blog! #Super',
  59. _id: 532caa3841176afb4a7c8476,
  60. created: {} // SHOULD THIS BE INCLUDED??
  61. }
  62.  
  63. Blog.findOne({
  64. _id: id
  65. }, 'title', function(err, blog) {
  66. console.log(JSON.parse(JSON.stringify(blog))); // causes toJSON to be executed
  67. });
  68. // outputs:
  69. {
  70. title: 'My first blog! #Super',
  71. _id: 532caa3841176afb4a7c8476
  72. }
  73.  
  74. Blog.findOne({
  75. _id: id
  76. }, function(err, blog) {
  77. console.log(JSON.parse(JSON.stringify(blog))); // causes toJSON to be executed
  78. });
  79. // outputs:
  80. {
  81. title: 'My first blog! #Super',
  82. blog: 'This is my very first #blog! I hope you enjoy it. #WOOHOO',
  83. _id: 532cb63e25e4ad524ba17102,
  84. __v: 0,
  85. comments: [], // SHOULD THIS BE INCLUDED??
  86. created: {
  87. by: 'Joe',
  88. date: Fri Mar 21 2014 17: 59: 26 GMT - 0400(EDT)
  89. }
  90. }
Add Comment
Please, Sign In to add comment