Can't update database

Can't update database
0.0 0

#1

I’m working on the nightlife coordination app and I can’t seem to update an array of objects. I want to update some properties on a particular object and then save it, but when I check the database, it doesn’t show that it has been updated.
Here is my model, and my server code:


The database query is at the very end of the server code. I did business.save() before but it threw an error saying that I can’t use the save function with $elemMatch, and to use “Model.update()” instead. I’ve reached a very tough obstacle, and have been trying to solve this issue all day. If someone can help, I would greatly appreciate it.


#2

Can you provide an example of a database record?


#3

Sorry for the late reply, just now waking up. This is one of my database records

{
    "_id": {
        "$oid": "58b14b420739463e686fcdd4"
    },
    "location": "New Paltz",
    "query": "bars",
    "list": [
        {
            "loc": "New Paltz",
            "query": "bars",
            "attendance": 0,
            "users": [],
            "location": {
                "state_code": "NY",
                "coordinate": {
                    "longitude": -74.0858896114037,
                    "latitude": 41.7488792352997
                },
                "address": [
                    "21 Church St"
                ],
                "country_code": "US",
                "postal_code": "12561",
                "geo_accuracy": 9.5,
                "display_address": [
                    "21 Church St",
                    "New Paltz, NY 12561"
                ],
                "city": "New Paltz"
            }

What I want to do is update the attendance and user property of the objects.


#4

Couldn’t crack $elemMatch, do I decided to do a workaround:

app.post("/add-bar", jsonParser, (req, res) => {
    List.findOne(
      {location: req.body.data.location, query: req.body.data.query},
      (err, business) => {
        if (err) {
            throw err;
        }
        else {

          // Find index of the array containing the name of location
          let ind = -1;
          for (let i in business.list) {
            if (business.list[i].name === req.body.data.name) {
              ind = i;
              break;
            }
          }

          // Didn't find a name. Return with status 400.
          if (!~ind) return res.status(400).send();

          if (business.list[ind].users.indexOf(req.body.data.username) !== -1) {
              res.json({data: "Exists"});
          }
          else {
              business.list[ind].attendance++;
              business.list[ind].users.push(req.body.data.username);
              business.markModified("list");
              business.save()
              res.send();
          }
        }
    })
})

Works fine with a db with some mock data.


#5

I will give this a try, thank you so much! Before trying $elemMatch, I had tried using .forEach to match the name, but in hindsight that wouldn’t have worked anyway.


#6

Works!! Thanks a million!