Created
September 3, 2018 20:33
-
-
Save bekatom/59d5ae23352e0f47140d5b1bcc9fddc0 to your computer and use it in GitHub Desktop.
mongoUpdate $iterator deep nested arrays with Mongoose Raw query
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function justSample() { | |
try { | |
mongoose.connect(config.database.connection, mongoConnectOptions) | |
mongoose.connection.db.collection('tenants').update( | |
{ '_id': mongoose.Types.ObjectId(userId) }, | |
{ | |
// $set: { | |
// 'lease.$[l].rooms.$[r].items.$[i].title': 'Flooring' | |
// } | |
$push: { | |
'lease.$[l].rooms.$[r].items.$[i].images': { file: '11', comment: '222' } | |
} | |
}, | |
{ | |
arrayFilters: [ | |
{ 'l._id': mongoose.Types.ObjectId(leaseId) }, | |
{ 'r._id': mongoose.Types.ObjectId(room) }, | |
{ 'i._id': mongoose.Types.ObjectId(areaId) } | |
] | |
}, | |
(err, data) => { | |
if (err) { return res.badRequest(err) } | |
return res.ok({data}) | |
}) | |
} catch (error) { | |
return res.badRequest(error) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment