How to push an array element only if it is a insert How to push an array element only if it is a insert mongodb mongodb

How to push an array element only if it is a insert


Because the doc is being created in the insert case, you would just create the initial children array with that element:

{$setOnInsert: {children: [{age: 12}]}}