How can I filter the object in the List with Mongoose?

How can I filter the object in the List with Mongoose?

However, I have to reach the Scenario ID in all products at once without sending repeated queries.

There is an error in findAll, it does not allow me to enter the Array.

let getItems = await secondModel.findOne({
   item_scenarios.scenario_id: '1'
});

Model making is like this.

item_scenarios: [
  {
    _id: 6050545060f40107076f2755,
    scenario_id: '1',
    scenario_tree_point: [Array]
  },
  {
    _id: 6050546160f40107076f2756,
    scenario_id: '1',
    scenario_tree_point: [Array]
  },
  {
    _id: 6050549b60f40107076f2757,
    scenario_id: '1',
    scenario_tree_point: [Array]
  },
  {
    _id: 6050698e140a110a11365aad,
    scenario_id: '604f3376dd79d8118a6990fe',
    scenario_tree_point: [Array]
  }
],

Error : SyntaxError: Unexpected token '.'

Answers 1

  • The error message is referring to the dot in item_scenarios.scenario_id. They are not valid in object keys in JS. Wrapping the key in quotes should fix the error:

    let getItems = await secondModel.findOne({
       'item_scenarios.scenario_id': '1'
    });
    

Related Articles