3

I'm working on a school directory and I want to push objects to my locations array, which is a property of my child object Floor that belongs to a Parent Object called Map.

Floor Schema

const floorSchema = new mongoose.Schema({
    title:{
      type:String,
      required:true
    },
    file:{
      type:String,
      required:true
    },
    floorSlug:{
      type:String,
      required:true,
    },
    locations:[{
        locationTitle: String,
        locationSlug: String,
        description: String,
        position_x: String,
        position_y: String,
        position_z: String,
    }],
})

My route to add location objects stands like this which I tried to "edit the floor", specifically trying to push to the locations property:

router.patch('/:slug/:floorSlug/add-location', (req, res) => {
  const slug = req.params.slug;
  const { floorSlug } = req.body;
  const querySlug = '^' + slug + '$';
  const locations = req.body.locations;
  Map.findOneAndUpdate(
      {
        $and: [
          { "slug": { '$regex': querySlug, $options: 'i' } },
          { 'floorSlug': floorSlug }]
      },
      {
        $push: { "locations": locations }
      }
  )
  .then(map => {
    map.floors.push({
      locations
    })
    map.save()
      .then(map => res.json(map))
      .catch(err => res.status(400).json('Error: ' + err));
  })
  .catch(err => res.status(400).json('Error: ' + err))

});

My expected result should look like this:

    {
    "map": [
        {
            "floors": [
                {
                    "title": "upper level",
                    "file": "mapurl.com",
                    "floorSlug": "upper-level"
                    "locations": [
                        {
                            "locationTitle": "title",
                            "locationSlug": "title-1",
                            "description": "description",
                            "position_x": "1",
                            "position_y": "1",
                            "position_z": "1"
                        }
                    ]
                },
                {
                    "title": "lower level",
                    "file": "mapurl.com",
                    "floorSlug": "lower-level",
                    "locations": [
                        {
                            "locationTitle": "title",
                            "locationSlug": "title-1",
                            "description": "description",
                            "position_x": "1",
                            "position_y": "1",
                            "position_z": "1"
                        }
                        {
                            "locationTitle": "title",
                            "locationSlug": "title-2",
                            "description": "description",
                            "position_x": "2",
                            "position_y": "2",
                            "position_z": "2"
                        }
                    ]
                }
            ],
            "_id": "61cfb0911dfb0970a0eb00cb",
            "title": "map",
            "slug": "map-1",
        }
    ]
}

Thanks for your help!

1
  • 1
    Array.prototype.push takes element(s) as arguments that should be added to the array - looks like you're passing a callback function as the argument instead, which is incorrect. Commented Jan 12, 2022 at 0:23

1 Answer 1

1
+50

You can do it using $push.

I think this code may help you !

router.patch('/:slug/:floorSlug/add-location', (req, res) => {
  const slug = req.params.slug;
  const { floorSlug } = req.body;
  const querySlug = '^' + slug + '$';
  const locations = req.body.locations;
  Map.findOneAndUpdate(
    {
      $and: [
        { "slug": { '$regex': querySlug, $options: 'i' } },
        { 'floors.floorSlug': floorSlug }]
    },
    {
      $push: { 'floors.$.locations': locations }
    }
  );
});
Sign up to request clarification or add additional context in comments.

6 Comments

thanks a lot, this helped me find the locations property, but I want to push an object to the array of locations. how can I define that?
Using $push you can push the object in locations array !
thanks a lot, I have updated my code and I was wondering if im saving properly?
@AlbertoRamos i update my answer ! if my answer help you accept it !
this is still pushing a new a new object everytime I do a patch request. can you show me how to save it properly? Please check my updated code
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.