I have database architecture in MongoDB like below:
I want when delete one document in colection company_info, it must delete all document which have same com_id with deleted document in colection company_info.
For example, When I delete document with coimid = DHBK in colection company_info, mongo shell must delete all document in relationship collection (users,company_user_role,...) which has comid = DHBK
I don't know mongo shell to do this action.
Here is my database example:
Collection company_info:
{"_id":{"$oid":"5eb8bd2480e33d0f04876616"},"comid":"DHBK","comshortname":"DHQG DHBK","comfullname":"BK University","comaddress":"268 Lý Thường Kiệt HCM","comtel":"028123456789","compersoncontact":"Lê Tiến Thường","compersontel":"028123456789","compersonemail":"thuong#bk.com.vn","comstatus":"ACTIVE","comdate":"2020-05-11"}
{"_id":{"$oid":"5ebe02bcb4146803d8ad255f"},"comid":"DHKT","comshortname":"DHKT","comfullname":"University of Economics Ho Chi Minh City","comaddress":"59C Nguyễn Đình Chiểu, Phường 6, Quận 3, Hồ Chí Minh","comtel":"028123456789","compersoncontact":"Nguyễn Trần Thanh Lâm","compersontel":"028123456789","compersonemail":"thanhlam#bk.com.vn","comstatus":"ACTIVE","comdate":"2020-05-11"}
{"_id":{"$oid":"5ecccb3bb8d5570916cf2916"},"comid":"DHYD","comshortname":"DHYD","comfullname":"Đại học Y Dược TP.HCM","comaddress":"215 Hồng Bàng, Phường 11, Quận 5, Hồ Chí Minh","comtel":"028123456789","compersoncontact":"Donal Trump","compersontel":"028123456789","compersonemail":"trump#america.com.vn","comstatus":"ACTIVE","comdate":"2020-05-26"}
Users collection:
{"_id":{"$oid":"5ebd05b52f3700008500220b"},"username":"DHBK","password":"123456","lastname":"DHBK","useremail":"dhbk#edu.com.vn","usertel":"0907111001","userdate":"2020-05-05","userstatus":"ACTIVE","userparentid":null,"comid":"DHBK","comdepartment":"DHBK","usercode":"DHBK_0001","usertype":"ADMIN_COM"}
{"_id":{"$oid":"5ebd05b52f3700008500220c"},"username":"KHOA_DIEN","password":"123456","lastname":"KHOA_DIEN","useremail":"KHOA_DIEN#edu.com.vn","usertel":"0907111002","userdate":"2020-05-05","userstatus":"ACTIVE","userparentid":"DHBK","comid":"DHBK","comdepartment":"KHOA_DIEN","usercode":"DHBK_0002","usertype":"USER_COM"}
{"_id":{"$oid":"5ebd05b52f3700008500220d"},"username":"KHOA_XD","password":"123456","lastname":"KHOA_XD","useremail":"KHOA_XD#edu.com.vn","usertel":"0907111003","userdate":"2020-05-05","userstatus":"ACTIVE","userparentid":"DHBK","comid":"DHBK","comdepartment":"KHOA_XD","usercode":"DHBK_0003","usertype":"USER_COM"}
{"_id":{"$oid":"5ebd05b52f3700008500220e"},"username":"KHOA_CNTT","password":"123456","lastname":"KHOA_CNTT","useremail":"KHOA_CNTT#edu.com.vn","usertel":"0907111004","userdate":"2020-05-05","userstatus":"ACTIVE","userparentid":"DHBK","comid":"DHBK","comdepartment":"KHOA_CNTT","usercode":"DHBK_0004","usertype":"USER_COM"}
Collection Company_user_role
{"_id":{"$oid":"5eb8c6d580e33d0f0487661a"},"rolecode":"DHBK_ROLE_01,DHBK_ROLE_02,DHBK_ROLE_03","comid":"DHBK","username":"DHBK","datecreate":"2020-05-13"}
{"_id":{"$oid":"5ebe01b5b4146803d8ad255c"},"rolecode":"DHBK_ROLE_01,DHBK_ROLE_03","comid":"DHBK","username":"KHOA_DIEN","datecreate":"2020-05-13"}
{"_id":{"$oid":"5ebe020ab4146803d8ad255d"},"rolecode":"DHKT_ROLE_01,DHKT_ROLE_02","comid":"DHKT","username":"DHKT","datecreate":"2020-05-13"}
{"_id":{"$oid":"5ebe025ab4146803d8ad255e"},"rolecode":"DHKT_ROLE_02","comid":"DHKT","username":"GV_BMKTTHUE","datecreate":"2020-05-13"}
Collection company_role:
{"_id":{"$oid":"5eb8c41830cb8651e0906d7c"},"rolecode":"DHBK_ROLE_01","rolename":" ROLE 01","rolelevel":"1","roledesc":"ROLE 01","rolestatus":"ACTIVE","roledate":"2020-05-11","comid":"DHBK"}
{"_id":{"$oid":"5eb8c42630cb8651e0906d7d"},"rolecode":"DHBK_ROLE_02","rolename":" ROLE 02","rolelevel":"1","roledesc":"ROLE 02","rolestatus":"ACTIVE","roledate":"2020-05-11","comid":"DHBK"}
{"_id":{"$oid":"5ebdff28b4146803d8ad2556"},"rolecode":"DHBK_ROLE_03","rolename":" ROLE 03","rolelevel":"1","roledesc":"ROLE 03","rolestatus":"ACTIVE","roledate":"2020-05-11","comid":"DHBK"}
{"_id":{"$oid":"5ebdff46b4146803d8ad2557"},"rolecode":"DHKT_ROLE_01","rolename":" ROLE 01","rolelevel":"1","roledesc":"ROLE 01","rolestatus":"ACTIVE","roledate":"2020-05-11","comid":"DHKT"}
{"_id":{"$oid":"5ebdff55b4146803d8ad2558"},"rolecode":"DHKT_ROLE_02","rolename":" ROLE 02","rolelevel":"1","roledesc":"ROLE 02","rolestatus":"ACTIVE","roledate":"2020-05-11","comid":"DHKT"}
Collection Company_function:
{"_id":{"$oid":"5eb8c35b30cb8651e0906d79"},"functioncode":"DHBK_FUNC_01","functionname":"QUYEN 01","functiondesc":"QUYEN 01","functionstatus":"ACTIVE","functiondate":"2020-05-11","comid":"DHBK"}
{"_id":{"$oid":"5eb8c36b30cb8651e0906d7a"},"functioncode":"DHBK_FUNC_02","functionname":"QUYEN 02","functiondesc":"QUYEN 02","functionstatus":"ACTIVE","functiondate":"2020-05-11","comid":"DHBK"}
{"_id":{"$oid":"5eb8c37830cb8651e0906d7b"},"functioncode":"DHBK_FUNC_03","functionname":"QUYEN 03","functiondesc":"QUYEN 03","functionstatus":"ACTIVE","functiondate":"2020-05-11","comid":"DHBK"}
{"_id":{"$oid":"5ebdfedbb4146803d8ad2554"},"functioncode":"DHKT_FUNC_01","functionname":"QUYEN 01","functiondesc":"QUYEN 01","functionstatus":"ACTIVE","functiondate":"2020-05-11","comid":"DHKT"}
{"_id":{"$oid":"5ebdfef6b4146803d8ad2555"},"function_code":"DHKT_FUNC_02","function_name":"QUYEN 02","function_desc":"QUYEN 02","function_status":"ACTIVE","function_date":"2020-05-11","com_id":"DHKT"}
Collection Company_role_function:
{"_id":{"$oid":"5eb8c55230cb8651e0906d7e"},"rolecode":"DHBK_ROLE_01","functioncode":"DHBK_FUNC_01, DHBK_FUNC_02","productid":"mBaaS_Platform","comid":"DHBK"}
{"_id":{"$oid":"5eb8c5a030cb8651e0906d7f"},"rolecode":"DHBK_ROLE_02","functioncode":"DHBK_FUNC_02, DHBK_FUNC_03","productid":"GIS_Platform","comid":"DHBK"}
{"_id":{"$oid":"5ebe0016b4146803d8ad2559"},"rolecode":"DHKT_ROLE_01","functioncode":"DHKT_FUNC_01, DHKT_FUNC_02","productid":"mBaaS_Platform","comid":"DHKT"}
{"_id":{"$oid":"5ebe003cb4146803d8ad255a"},"rolecode":"DHKT_ROLE_02","functioncode":"DHKT_FUNC_01","productid":"Analysis_Platform","comid":"DHKT"}
{"_id":{"$oid":"5ebe00ffb4146803d8ad255b"},"rolecode":"DHBK_ROLE_03","functioncode":"DHBK_FUNC_01, DHBK_FUNC_02,DHBK_FUNC_03","productid":"IOT_Platform","comid":"DHBK"}
Collection Company_product
{"_id":{"$oid":"5eb8c09180e33d0f04876618"},"comid":"DHBK","productid":"GIS_Platform","contractcode":"DHBK_CONTRACT_1","contractdate":"2020-05-05","contractstatus":"ACTIVE","datecreate":"2020-05-06"}
{"_id":{"$oid":"5eb8c0c580e33d0f04876619"},"comid":"DHBK","productid":"mBaaS_Platform","contractcode":"DHBK_CONTRACT_2","contractdate":"2020-05-05","contractstatus":"ACTIVE","datecreate":"2020-05-15"}
{"_id":{"$oid":"5ebdfda8d57326595c694904"},"comid":"DHKT","productid":"mBaaS_Platform,Analysis_Platform","contractcode":"DHKT_CONTRACT_1","contractdate":"2020-05-05","contractstatus":"ACTIVE","datecreate":"2020-05-15"}
{"_id":{"$oid":"5ec78a66b8d5570916cbc0b3"},"comid":"DHBK","productid":"ORIMX_platform","contractcode":"22052020-TEST","contractdate":"2020-05-20","contractstatus":"ACTIVE","datecreate":"2020-05-22"}
{"_id":{"$oid":"5ecc7987b8d5570916ceda21"},"comid":"DHBK1","productid":"ORIMX_platform","contractcode":"11111111","contractdate":"22052020","contractstatus":"DISABLE","datecreate":"22052020"}
As of now, Mongo doesn't support cascade delete operation. Mongo just does what it has been told to do. Cascade delete is not a concept in MongoDB, it's more of a concept in SQL.
So, for now what you can do is, you can manually delete the matching record in every collection. You can use the below to delete a document:
db.Users.remove({coimid: 'DHBK'})
db.Company_user_role.remove({coimid: 'DHBK'})
... // And so on...
There is another way, but, it's a long route. What you can do is change your schema. Instead of making different collection, you can put everything in just one collection. But, I would recommend you to do this only if your collections are connected and it's frequently updating.
I am creating an iOS application to help users who collect figurines keep track of their figurines and see realtime price information.
I have two collections:
The "master" collection
This is essentially a catalog of thousands of figurines users can select to add to their collection. It has information about each item, such as Artist, Original Price, Barcode, Year, Comments, etc.
The "user" collection
This contains documents for each user. Each of these "user" documents has a sub-collection with documents for each figurine they have added to their collection.
Currently, if a user wants to add a new item to their personal collection, they go to the "master" collection UITableView and swipe to add an item. The item document is basically copied from the "master" collection and a new document in the user sub-collection is created. They can then go to a UITableView that displays the items they have added. They have the option to add their own information about the item, such as comments, date bought, date sold, price bought, etc.
My question: How can I sync information between the document in the user collection/sub-collection and the document in the main collection? Say the "Market Price" for an item has gone up, so I update the main collection database. How can I get this price change to display in all users' collections?
I assume by using Cloud Functions, but I wanted to post here in case there was an easier method.
This is definitely a good use for Cloud Functions. Create an onUpdate trigger for your /users{uid}/figurines/{figurineId} collection and then use that to copy the updated data to the master.
You can either copy specific fields or you can check for differences by examining event.data.data() and event.data.previous.data()
const admin = require('firebase-admin');
const functions = require('firebase-functions');
admin.initializeApp(functions.config().firebase);
const db = admin.firestore();
exports.updateMaster = functions.firestore
.document('users/{uid}/figurines/{figurineId}')
.onUpdate(event => {
var newData = event.data.data();
var oldData = event.data.previous.data();
var updateData = {};
if (oldData.price != newData.price) {updateData.price = newData.price}
// Add any more changes that you may want to compare / update below...
// Then...
return db
.collection('master')
.doc(event.params.figurineId)
.update(updateData).then(response => {
console.log('We updated figurine', event.params.figurineId);
}).catch(err => {
console.error('There was an error', err);
});
});