These are chat archives for Automattic/mongoose

8th
Jan 2016
Valeri Karpov
@vkarpov15
Jan 08 2016 15:03
@httpete-ire just put your question in here or in github issues, I typically respond to questions in this room in batch
Pete Redmond
@httpete-ire
Jan 08 2016 15:36

@vkarpov15 great, so what I'm trying to do is insert a new embedded document called Chapter. I want to ensure that the Chapter is unique by a title attribute, also only the owner of the parent document can insert a Chapter.

  let chapter = new Chapter();
  chapter.title = req.body.title;
  chapter.content.markdown = req.body.markdown;
  chapter.content.html = req.body.html;

  let query = Doc.update({
    $and: [
      {
        _id: docId
      },
      {
        owner: req.user
      },
      {
        'chapters.title': {
          $ne: chapter.title
        }
      }
    ]
  },
  {
    $push: {
      chapters : chapter
    }
  },
  updateOptions);

  query.exec().then(function(value) {

    if(value.nModified === 0) {
      return res.sendStatus(400);
    }

    return res.sendStatus(200);
  });

This works but what I'm struggling with is how to handle a) the parent document is not found (I would like to return a 404 and an error message) and b) a Chapter already exists with the same title (I would like to return a 409 status and an error message).

As you can see I have a generic error returning if no document is modified.

Is it possible to handle the errors the way I would like or should I structure my query differently. Thanks for the help :)

Dipankar Arhat
@dbelieves
Jan 08 2016 15:41

hey everyone, i am stuck on a very trivial thing:
i am using meanjs.
i have a form which sends data 'req' to server controller.
my mongoose schema is nested.

this is the request i am sending

{ latitude: 1, longitude: 909, ownername: 'panda', rent: 124 }

how do i format it to this type
'''
var HouseSchema = new Schema({

meta: {

    created: {
        type: Date,
        default: Date.now
    },

    coordinates: {
        latitude: {
            type: String,
            default: ''
        },

        longitude: {
            type: String,
            default: ''
        }

    },

    locality: {
        type: String,
        default: ''
    },

    address: {
        type: String,
        default: ''
    },

    rent: {
        type: Number,
        default: ''
    },

    owner_details: {

        contact_number: {
            type: Number,
            default: ''
        },

        address: {
            type: String,
            default: ''
        },
        owner_name: {

            type: String,
            default: ''
        },

    }

}

});
'''

any help ?
Valeri Karpov
@vkarpov15
Jan 08 2016 16:49
@httpete-ire unfortunately its difficult to distinguish between a and b, because mongodb will just return no results in both cases. The only way I really know of to achieve this is using findOneAndUpdate() and $addToSet. Or you can explicitly overwrite chapters instead of pushing.
@dbelieves can you clarify your question? I don't understand
Dipankar Arhat
@dbelieves
Jan 08 2016 16:50
so i am using meanjs
@vkarpov15
when a user sends a form; the data in it is in the form like this:
{ latitude: 1, longitude: 909, ownername: 'panda', rent: 124 }
but in my mongoose schema rent variable has to be stored in , house.meta.rent
how do i do this ?
also ownername has to be stored in house.meta.owner_details.owner_name
Dipankar Arhat
@dbelieves
Jan 08 2016 17:05
got it working...
Valeri Karpov
@vkarpov15
Jan 08 2016 18:44
the brute force solution is something like house.meta.rent = req.body.rent; for each of the fields
you can DRY it up with a loop, or just modify how the client sends data to match your schema