You've got couple of options :
On MongoDB v4.2 or above && If you're ok to store MongoDB's _id
's string value(which is unique) try this ::
As .updateMany() in latest version accepts aggregation pipeline :
Site.updateMany(
// Filtering documents to check siteID not exists & equals to null (just null check will make sure field doesn't exists as well)
{ siteID: null },
[
{ $set: { siteID: { $toString: '$_id' } } }
])
On MongoDB below v4.2:
You need to first fetch and then update :
a) Fetch documents :
let response = Site.aggregate([{$match : {date : null}}, {$project :{siteID : {$toString : '$_id'}}}])
b) Update documents :
let bulkArr = [];
response.forEach(element => {
bulkArr.push({
updateOne: {
"filter": { _id: element._id }, // Use mongoose.Types.ObjectId(element._id) if needed.
"update": { '$set': { 'siteID': element.siteID } }
}
})
});
let updateResult = await Site.bulkWrite(bulkArr)
console.log('matchedCount ::', updateResult.matchedCount, 'modifiedCount ::', updateResult.modifiedCount)
Just in case if you don't want to store string value of _id
, then in the Step a)
you can just get _id
's projected & in Step b)
Create random string using :: generate-random-string-characters-in-javascript while iterating through array from Step 1)
.