Error - The positional operator did not find, Mongo db update command not working in Meteor

My stored doc in mongo is :

[{
  _id: 'Bd5r6vmb6bu5R2chQ',
  run_status: true,
  start_time: 2024-06-23T20:19:12.000Z,
  stop_time: 2024-06-23T20:19:12.000Z,
  strategies: [],
  summary: '{"pnl_per":0,"pnl":0,"profit":0,"loss":0,"win":0,"lose":0,"win_per":0,"ppt":0}',
  result: {}
}]

I am using meteor to develop the application with the built in mongo libs.

Now when I am trying to update the existing doc with some new attribute - “flag”: true. But ending up getting error :

(node:81184) UnhandledPromiseRejectionWarning: MongoServerError: The positional operator did not find the match needed from the query.

Here is my code to update the doc :

        let doc = LiveStrategies.find({}, {sort: {start_time: -1, limit: 1}}).fetch()[0];
        LiveStrategies.update( { _id: doc._id }, { $set: { flag: true } },
            (updateErr) => {
               if (updateErr) {
                  console.error('Error updating document:', updateErr);
                  return;
               }
                  console.log('Document updated successfully.');
               }
            }
        );

When i test with find query, its working perfectly fine and getting the doc correctly.

Appreciate your help on resolving this.

Although this seems not related to the error but first

Use findOne() instead of find().fetch()[0]

1 Like

My schema is :

StrategySchema = new SimpleSchema({
    name: {
        type: String,
        label: 'Algo Name'
    },
    instrument: {
        type: String,
        label: 'Instrument'
    }
});
LiveStrategySchema = new SimpleSchema({
    start_time: {
        type: Date,
        label: 'Start Time'
    },
    stop_time: {
        type: Date,
        label: 'Stop Time'
    },
    strategies: {
        type: Array,
        label: 'Strategies',
        optional: true
    },
    "strategies.$": StrategySchema
}

When I am setting the strategies attribute to Object type, not getting any error and update is working fine. But the strategies value is setting to empty.

Is the following correct way to define array of object :

strategies: {
        type: Array,
        label: 'Strategies',
        optional: true
    },
    "strategies.$": {
        type : StrategySchema,
        optional : true
    }

Finally i got the root cause : My StrategySchema schema is in diff file and not imported correctly due to which ending up with error while updating/inserting doc in to mongo db.