## add new entity type
```json=
db.getCollection("entityTypes").insert({
"entityTypeId": 11,
"isMergeEligible": false,
"name": "API user"
})
```
# Entities update
- db update
```json=
db.entities.updateMany({},
[{$set: {
'scope.workspaceIds': [],
'scope.projectIds': [],
'scope.organizationIds': '$organizationIds'
}}])
```
- workspaceEntities
```json=
let result = db.workspaceEntities.aggregate([{
$match: {workspaceId: {$exists: true },
}},
{
$group: {
_id: { workspaceId: '$workspaceId' },
entityIds: {$push: '$entityId' }
}
},
{
$lookup: {
from: 'workspaces',
localField: '_id.workspaceId',
foreignField: 'workspaceId',
as: 'workspace'
}},
{
$lookup: {
from: 'projects',
localField: 'workspace.projectId',
foreignField: 'projectId',
as: 'project'
}},
]
).toArray()
for(let item of result) {
db.entities.updateMany({entityId: {$in: item.entityIds}},
{$addToSet: {
'scope.workspaceIds': item._id.workspaceId,
'scope.projectIds': item.project[0].projectId,
'scope.organizationIds': item.project[0].organizationId
}})
}
```
- workspaceSystemUsers
```json=
let result =
db.workspaceSystemUsers.aggregate([{
$match: {workspaceId: {$exists: true },
}},
{
$group: {
_id: { workspaceId: '$workspaceId' },
systemUserIds: {$push: '$systemUserId' }
}
},
{
$lookup: {
from: 'systemUsers',
localField: 'systemUserIds',
foreignField: 'systemUserId',
as: 'systemUsers'
}},
{
$lookup: {
from: 'workspaces',
localField: '_id.workspaceId',
foreignField: 'workspaceId',
as: 'workspace'
}},
{
$lookup: {
from: 'projects',
localField: 'workspace.projectId',
foreignField: 'projectId',
as: 'project'
}}]
)
.toArray()
for(let item of result) {
let entityIds = item.systemUsers.map(item => item.entityId)
let query = {'scope.workspaceIds': item._id.workspaceId}
if(item.project[0]){
query = {
...query,
'scope.projectIds': item.project[0].projectId,
'scope.organizationIds': item.project[0].organizationId
}
}
db.entities.updateMany({entityId: {$in: entityIds}},
{$addToSet: query})
}
```
- projectSystemUsers
```json=
let result =
db.projectSystemUsers.aggregate([{
$match: {projectId: {$exists: true },
}},
{
$group: {
_id: { projectId: '$projectId' },
systemUserIds: {$push: '$systemUserId' }
}
},
{
$lookup: {
from: 'systemUsers',
localField: 'systemUserIds',
foreignField: 'systemUserId',
as: 'systemUsers'
}},
{
$lookup: {
from: 'projects',
localField: '_id.projectId',
foreignField: 'projectId',
as: 'project'
}},
]
)
.toArray()
for(let item of result) {
let entityIds = item.systemUsers.map(item => item.entityId)
let query = {}
if(item.project[0]){
query = {
...query,
'scope.projectIds': item.project[0].projectId,
'scope.organizationIds': item.project[0].organizationId
}
}
if(Object.keys(query).length === 0) {
continue
}
db.entities.updateMany({entityId: {$in: entityIds}},
{$addToSet: query})
}
```
- organizationSystemUsers
```json=
let result =
db.organizationSystemUsers.aggregate([{
$match: {organizationId: {$exists: true },
}},
{
$group: {
_id: { organizationId: '$organizationId' },
systemUserIds: {$push: '$systemUserId' }
}
},
{
$lookup: {
from: 'systemUsers',
localField: 'systemUserIds',
foreignField: 'systemUserId',
as: 'systemUsers'
}}
]
)
.toArray()
for(let item of result) {
let entityIds = item.systemUsers.map(item => item.entityId)
db.entities.updateMany({entityId: {$in: entityIds}},
{$addToSet: { 'scope.organizationIds': item._id.organizationId}})
}
```
## Draft
```json=
let result =
db.entities.find({entityTypeIds:[null]})
.toArray()
// function searchAll(query) {
// var all = db.getCollectionNames();
// var results = [];
// all.forEach(function(collectionName) {
// if (db[collectionName]) console.log(`Collection name ${collectionName}`,db[collectionName].find(query).toArray());
// });
// return results;
// }
let entityids = result.map(item => item.entityId)
db.entities.deleteMany({entityId: entityids})
// searchAll({ entityId: {$in: entityids}})
// messageEntityUnread
// systemUsers
// workspaceEntities
// channels
// const result = db.entities.aggregate([
// { $match: { entityTypeIds:[null]}}
// ,
// {
// $lookup: {
// from: 'messageEntityUnread',
// localField: 'entityId',
// foreignField: 'entityId',
// as: 'messageEntityUnread'
// }
// },
// {
// $lookup: {
// from: 'systemUsers',
// localField: 'entityId',
// foreignField: 'entityId',
// as: 'systemUsers'
// }
// },
// {
// $lookup: {
// from: 'workspaceEntities',
// localField: 'entityId',
// foreignField: 'entityId',
// as: 'workspaceEntities'
// }
// },
// {
// $lookup: {
// from: 'channels',
// localField: 'entityId',
// foreignField: 'entityId',
// as: 'channels'
// }
// }
// ]).toArray()
// for(let item of result){
// if(item.messageEntityUnread.length > 0){
// db.messageEntityUnread.deleteMany({entityId: item.entityId})
// }
// if(item.systemUsers.length > 0){
// db.systemUsers.deleteMany({entityId: item.entityId})
// }
// if(item.workspaceEntities.length > 0){
// db.workspaceEntities.deleteMany({entityId: item.entityId})
// }
// if(item.channels.length > 0){
// db.channels.deleteMany({entityId: item.entityId})
// }
// }
```