0

I'm trying to follow the filtered positional operator $ to update an a field in a target subdocument stored in array like this :

trainings : [ { "title": "bonjour", "category": "clochard", "_id": "5a3bee028e3f660b30382a21" }, { "title": "Controlleur", "category": "Métro sahel", "_id": "5a3c0378c52add202218b199" }, { "title": "Controlleur", "category": "Métro Tunis", "_id": "5a3c0380c52add202218b19a" }, { "title": "Takwira", "category": "sport", "_id": "5a3c038ac52add202218b19b" } ]

This is my query implemented with mongoose (trainings is a field in User model):

User.findByIdAndUpdate(user._id, { $set: { "trainings.$[element].content": req.body } },
                 { arrayFilters: [ { "element._id": req.params.courseId } ],upsert: true},function (err, updatedTrainings)
             {
               if (err)
               {
                 console.log(err);
               }
               res.json({success:true,message:"Training updated succfully",body:updatedTrainings});
             }); 

I get this error:

{ MongoError: exception: cannot use the part (trainings of trainings.$[element].content) to traverse the element ({trainings: [ { slugs: [], createdAt: new Date(1513876994108), _id: ObjectId('5a3bee028e3f660b30382a21'), content: { title: "bonjour", category: "clochard" }, path: "bonjour-clochard-664" }, { slugs: [], createdAt: new Date(1513882488053), _id: ObjectId('5a3c0378c52add202218b199'), content: { title: "Controlleur", category: "Métro sahel" }, path: "Controlleur-Métro sahel-505" }, { slugs: [], createdAt: new Date(1513882496517), _id: ObjectId('5a3c0380c52add202218b19a'), content: { title: "Controlleur", category: "Métro Tunis" }, path: "Controlleur-Métro Tunis-887" }, { slugs: [], createdAt: new Date(1513882506039), _id: ObjectId('5a3c038ac52add202218b19b'), content: { title: "Takwira", category: "sport" }, path: "Takwira-sport-833" } ]}) at Function.MongoError.create (/home/developer/takwinland/backend/app/node_modules/mongodb-core/lib/error.js:31:11) at /home/developer/takwinland/backend/app/node_modules/mongodb-core/lib/connection/pool.js:497:72 at authenticateStragglers (/home/developer/takwinland/backend/app/node_modules/mongodb-core/lib/connection/pool.js:443:16) at Connection.messageHandler (/home/developer/takwinland/backend/app/node_modules/mongodb-core/lib/connection/pool.js:477:5) at Socket. (/home/developer/takwinland/backend/app/node_modules/mongodb-core/lib/connection/connection.js:331:22) at emitOne (events.js:125:13) at Socket.emit (events.js:221:7) at addChunk (_stream_readable.js:265:12) at readableAddChunk (_stream_readable.js:252:11) at Socket.Readable.push (_stream_readable.js:209:10) at TCP.onread (net.js:598:20)

  • Can you run db.version() in shell to confirm mongo version ? – s7vr Dec 22 '17 at 10:55
  • Hello this the version of mongo db :> db.version() 2.6.10 – Med Dhaker Abdeljawed Dec 22 '17 at 11:11
  • Multi array updates functionality is available from 3.6 version – s7vr Dec 22 '17 at 11:13
  • If I migrate to mongodb 3.6, should I update mongoose version ? the current version is 4.11.12 ! – Med Dhaker Abdeljawed Dec 22 '17 at 11:16
  • Array filters are changes on server side. It should work fine but for rest of 3.6 changes you may have to. Not sure if mongoose has a 3.6 compatible release out already. You should get the latest anyways. – s7vr Dec 22 '17 at 11:20
  • just a last question, Is there anyway to handle this query (update a field of a sub document in an array of sub documents) in earlier versions? – Med Dhaker Abdeljawed Dec 22 '17 at 11:25
  • Possible duplicate of [How to Update Multiple Array Elements in mongodb](https://stackoverflow.com/questions/4669178/how-to-update-multiple-array-elements-in-mongodb) – s7vr Dec 22 '17 at 11:27
  • 1
    Actually looking at your data it looks like you don't need the multi positional operator. Regular single positional query should work for you. Try `User.update({_id:user._id, "trainings._id".req.params.courseId"}, { $set: { "trainings.$.content": req.body } }, {upsert: true}` – s7vr Dec 22 '17 at 11:41
  • It worked fine and updated the subdocument, but didn't trigger the the update middleware which i defined in the subdoc schema, – Med Dhaker Abdeljawed Dec 22 '17 at 14:01

0 Answers0