0

Using the npm package mongojs, I need to access nested objects using a variable. The following, however, does not work:

    let userID = req.user._id,
        marketName = req.body.marketName,
        itemName = req.body.item.itemName,
        crossOff = req.body.item.isCrossed;

    markets.updateOne(
       {
        _id: userID,
        "marketList.marketName": marketName
      },
      {$set : {`marketList.$.shoppingList.${itemName}.isCrossed`: crossOff}},
      (err, doc) => {
        if(err) { res.status(400).send('Bad Request') }
        else { res.status(204) }
        res.end();
      });
   });

As you cannot use template strings in this situation, hence the unexpected template string error.

I am aware that it is possible to use bracket-notation to use a variable as a key, however, this will not help my situation since I am unable to set a variable with the entire key stored in it as a string.

To help, here is a sample the document structure:

{
  _id: ObjectId(...),
  marketList: [
    { marketName: 'ralphs',
      shoppingList: {
        "cookies": {  itemName: "cookies", isCrossed: false },
        "moar cookies": { itemName: "moar cookies", isCrossed: true }
    },
    {
      marketName: 'gelsons',
      shoppingList: {
        "even moar cookies": { itemName: "even moar cookies", isCrossed: true }
      }
    }
  ]
}

What are my options?

5
  • 1
    Why are you unable to just create an object and use brackets for the key? Template strings are just syntactic sugar, if you can use those, you can use an object. Commented May 21, 2017 at 21:53
  • @adeneo I have no idea what the location of the marketName is ahead of time. Otherwise I would simply use bracket notation. Commented May 21, 2017 at 21:58
  • As in -> jsfiddle.net/adeneo/j7tc7muL/1 Commented May 21, 2017 at 22:00
  • Well I'll be damned. Thank you @adeneo I did not know I could set a query in such a way. Commented May 21, 2017 at 22:08
  • You're welcome, I added it as an answer as well. Commented May 21, 2017 at 22:36

1 Answer 1

2

You can create the query in advance, that way bracket notation can be used for the key

let userID   = req.user._id,
  marketName = req.body.marketName,
  itemName   = req.body.item.itemName,
  crossOff   = req.body.item.isCrossed,
  query      = {};

query["marketList.$.shoppingList." + itemName + ".isCrossed"] = crossOff;

markets.updateOne({
        _id: userID,
        "marketList.marketName": marketName
    }, {
        $set: query
    }, (err, doc) => {
        if (err) {
        res.status(400).send('Bad Request');
        } else {
        res.status(204);
        }
        res.end();
    });
});
Sign up to request clarification or add additional context in comments.

Comments

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.