Please review this... nested document save and retrieval

13 views
Skip to first unread message

Raja Malik

unread,
Jul 6, 2015, 10:56:34 AM7/6/15
to mongoo...@googlegroups.com
Hello All,

I am again posting this question with full source code. Please help me.

Here is my invite.model.js

'use strict';

var moment = require('moment'),
    mongoose = require('mongoose'),
    Schema = mongoose.Schema;

var CommentSchema = new Schema({
  author     : String
  , text      : String
  , date      : Date
});

var InviteSchema = new Schema({
  _id:{type:String,required:true},
  occasion: {type:String,required:true},
  date: {type:String, required:true},
  welcomeMsg: {type:String,required:false},
  host:{type:String,required:true},
  hostId:{type:String,required:true},
  guests:{type:[String],required:true},
  rsvpYes:{type:[String],required:false},
  rsvpNo:{type:[String],required:false},
  venue:{
      searchString:{type:String,required:true},
      lat: {type:String,required:true},
      lng: {type:String,required:true},
      place: {type:String}
  },
  comments: {type:[CommentSchema]},
  yearly:Boolean,
  cancelled:Boolean,
  reason:String,
  updatedDate:Date
}, {autoIndex:false});


module.exports = mongoose.model('Invite', InviteSchema);


Once i save this, this is what i get to see:

> db.invites.find().pretty()

{

"_id" : "150702005149198",

"occasion" : "Birthday Party",

"welcomeMsg" : "hello, please come.",

"date" : "Thu Jul 30 2015 20:00:00 GMT+0530",

"hostId" : "150702004953095",

"host" : "Host Name",

"cancelled" : false,

"comments" : [

"{\"text\":\"Hello, I will not be able to come to this party.\",\"author\":\"Raja Malik\"}",

"{\"text\":\"This is the comment I am not getting from DB.\",\"author\":\"Raja Malik\"}"

],

"venue" : {

"lat" : "28.660965148895627",

"lng" : "77.36326575279236",

"searchString" : "Some Place name",

"place" : "some other place"

},

"rsvpNo" : [ ],

"rsvpYes" : [ ],

"guests" : [

"a...@gmail.com"

],

"__v" : 0

}


First thing, I feel the comments saved in invite collection are strings rather than JSON object, if yes, then why is it happening like this.

Retrieval Code:
var query = req.query.iam === 'guest' ? {guests:{$in: [req.query.guest]},cancelled:false}
    : {hostId:req.query.host,cancelled:false};
  Invite.find({$query:query, $orderby:{_id : -1}}, function (err, invites) {

    //in this invites[0].comments - I get empty array []
    if(err) {
      log.error({erorFetchingUsersInvites: err});
      return handleError(res, err);
    }
    if(!invites) { return res.send(404); }
      return res.json(201, {data:invites});
  });


Please help.

Thanks
Raja

Reply all
Reply to author
Forward
0 new messages