1

I'm trying to add an object in a specific object inside an array of objects.

My model:

[
    {
        "_id": "634c0777427df2a5adc17b4c",
        "title": "PROGRAMME TEST",
        "user": {
            "name": "Jérémy",
            "email": "[email protected]",
            "profilPicture": "google.fr"
        },
        "exercices": [
            {
                "exercice": "6349563c2e0df123096b7feb",
                "totalSet": 1,
                "rest": "1"
                // add value here 
            },
            {
                "exercice": "634956452e0df123096b7fed",
                "totalSet": 2,
                "rest": "2"
            }
        ]
    }
]

Here is my query. It does nothing (not even an error):

return this.programModel.findOneAndUpdate(
  {
    $and: [
      {_id: '634c0777427df2a5adc17b4c' }, (program id)
      { exercices: { 'exercices.exercice': "6349563c2e0df123096b7feb" } }, (exercice id want I to add my object)
    ]
  },
  { $push: {"exercices.exercice": {result: result}}},
  { new: true }
);

EDIT:

Here is my entire program.entity.ts in case it can help.

unfortunately, none of the answers in the comment did work.

As I'm not familiar with mongoose and nestJS, Might I did a mistake in my entity?

export type ProgramDocument = Program & Document

class Exercices {
  @Prop({ type: mongoose.Schema.Types.ObjectId, ref: 'Exercice' })
  exercice: Exercice

  // others @Prop
  
  @Prop({ type: mongoose.Schema.Types.Array})
  result: Array<ExerciceResult>
}

class ExerciceResult {
  @Prop()
  set: number;

  @Prop()
  kilogram: string;
}


@Schema()
export class Program {
  // others @Prop

  @Prop({ type: mongoose.Schema.Types.Array})
  exercices: Array<Exercices>

}

export const ProgramSchema = SchemaFactory.createForClass(Program)
8
  • Can you try the $push condition as "exercices.$.exercice". Commented Oct 17, 2022 at 6:41
  • doesn't work. I know how to add a object inside the exercices[] but seems like it's not possible to go deeper than that. Commented Oct 17, 2022 at 8:11
  • The problem is looking like inside of $and condition. { exercices: { $elemMatch : {exercice: exerciceId} } }. or try as {'exercices.exercice' : mongoose.Types.ObjectId(exerciceId)}. Not inside of exercices object. Can you try one of these also. Commented Oct 17, 2022 at 9:12
  • I got this error : "plan executor error during findAndModify :: caused by :: Cannot create field 'exercice' in element {exercices: [ { exercice: "6349563c2e0df123096b7feb", totalSet: 1, rest: "1" }, { exercice: "634956452e0df123096b7fed", totalSet: 2, rest: "2" } ]}" The problem is when I add a exercice to a program (similar method than the one in the original post) it does save it as string and not "objectID(exerciceid)" in the database. Do you think the problem could be here ? Commented Oct 17, 2022 at 12:37
  • You may try to push value as ; {result: mongoose.Types.ObjectId(result)} Commented Oct 17, 2022 at 13:50

1 Answer 1

2
+50

Just need to correct an update part,

  • $ references the object that matches in the subdocument condition/query, so it would be "exercices.$.result"
let result = { set: 1, kilogram: "10" };

return this.programModel.findOneAndUpdate(
  {
    _id: "634c0777427df2a5adc17b4c",
    "exercices.exercice": "6349563c2e0df123096b7feb"
  },
  {
    $push: {
      "exercices.$.result": result
    }
  },
  { new: true }
);

Playground

Sign up to request clarification or add additional context in comments.

2 Comments

Great, it works perfectly. Thank you. How do you call the "$" in mongoose please ? I would like to find some documentations about it to learn how to use it properly.
It is a positional $ operator, you can find this in mongodb docs.

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.