Delete document matching condition in multi colection MongoDB - mongodb
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.
Related
Mongodb aggregation lookup with condition
I have two collections called 'store' and 'items' , where store keep all product records belongs to the supermarket with timestamp. And 'items' keeps records of some details about purchases. Store Collection {{"id": "802736816700002577966429","$date": "2021-05-05T07:02:51Z","state":"VALID"},{"id": "802736816700002577966430","$date": "2021-07-27T07:02:51Z","state":"NOT VALID"},{"id": "802736816700002577966430","$date": "2021-04-27T07:02:51Z","state":"VALID"}} Item Collection {{"id": "802736816700002577966429","$date": "2021-05-05T07:02:51Z"},{"id": "802736816700002577966430","$date": "2021-07-27T07:02:51Z"},{"id": "802736816700002577966430","$date": "2021-07-27T07:02:51Z"}} The query which i want to execute is -> Select items from Store Collection where state != VALID and timestamp<2021-05-10 and then delete corresponding items from Item Collection Please provide Spring-boot criteria/aggregation code which is similar to Criteria criteriaEvt = new Criteria(....); Aggregation aggregationEvt = newAggregation(.....)
MongoDB Morphia Update multi=true VS bulk update
I want to update 10,000 documents in a single web request. I intend to update only one field (which is indexed) in all the documents matched with some criteria with same value. I see morphia 1.3.2 always set multi=true parameter in update call. Is it enough for updating 10,000 document? Or there are any bulk update functionality in morphia.
The following code should work for you. Query<Entity> query = datastore.createQuery(Entity.class); query.filter("name = ", "xxx"); UpdateOperations<Entity> updateOperations = datastore.createUpdateOperations(Entity.class).set ("yyy", 200); UpdateResults updateResults = datastore.update(query, updateOperations, false, null); All the documents in the collection with name = 'xxx' will now have all their 'yyy' attribute equal to 200.
Doctrine ODM(mongo) - upsert embeded document if not exist
A mongo document exists which links Users and Cars. It contains the following fields: User.userId User.cars[] User.updated User.created User.cars is an array of embedded documents. A query needs to be written to only insert a Car to this field if it does not already contain a Car with id $carId The following query will create a new record each time for $userId and $carId. What it should do is either not insert a new record or update the value for driven. $qb ->findAndUpdate() ->upsert(true) ->field('userId')->equals($userId) ->field('cars.id')->notEqual($carId) ->field('cars')->addToSet([ 'id' => $carId, 'driven' => new \DateTime(), ]) ->field('updated')->set(new \DateTime()) ->field('created')->setOnInsert(new \DateTime()) ->limit(1) ; return $qb->getQuery()->execute(); Removing notEqual($carId) from the query will always insert the record into User.cars, which is also not desired. The end result should be that User.cars only contains one record for each carId.
If you don't need to search the array of cars later you could store it as an object instead and use $carId as key: $qb ->findAndUpdate() ->upsert(true) ->field('userId')->equals($userId) ->field("cars.$carId")->set([ 'id' => $carId, 'driven' => new \DateTime(), ]) ->field('updated')->set(new \DateTime()) ->field('created')->setOnInsert(new \DateTime()) ->limit(1) ; return $qb->getQuery()->execute(); If cars are mapped in the document as EmbedMany you may need to change your collection's strategy to either set or atomicSet (I'd suggest the latter) as otherwise ODM will reindex your array each time it's saved. Once again, this solution has its downsides which can be considered serious depending on how you want to use your data but it solves the problem in question. Offhand, ->limit(1) is superfluous as ->findAndUpdate() is making a query run findAndModify command which by its nature operates on single document.
Search for ObjectId of a document: pymongo
I want to access a document in collection by 'name' attribute for getting its ObjectId so that i can insert that unique objectid to other document for reference. cursorObject = db.collectionIngredient.find({'name': 'sugar'}) I want _id field of cursorObject. cursorObject.'_id' or cursorObject._id not working. I have tried __getitem__, __getattribute__ and so much internet surfing but couldn't able to find a way. Please help
First, as #jjmartinez pointed out, find returns a cursor, which you need to iterate over, in order to get hold of the documents returned by your query. The _id field belongs to the documents, not the cursor. I'm guessing that in your case the name is unique, so you can avoid cursor/iterating if you use find_one instead of find. Then you get the document directly. Then, to access the _id, you just need a standard dict-item access: id = doc['_id'] So we get: ingredient = db.collectionIngredient.find_one({'name': 'sugar'}) if ingredient is not None: id = ingredient['_id'] else: id = None
When you do cursorObject = db.collectionIngredient.find({'name': 'sugar'}) you have a collection of documents, not a single element. So you need to explore all the collection. You need to iterate inside the cursor: try: cursorObject = db.collectionIngredient.find({'name': 'sugar'}) except: print "Unexpected error:", sys.exc_info()[0] for doc in cursorObject: print doc Here you have the Pymongo Tutorial
Multi level MongoDB object querying by key
If you only know the key name (say "nickname"), but not the exact path to that key in the object. e.g. nickname may be at the first level like: {"nickname":"Howie"} or at the second level: {"user":{"nickname":"Howie"}} Is it possible to query for nickname equal "Howie" that would return both documents?
Unfortunately there is no wild card that allows you to search for a field at any level in the db. If the position is not relevant and you can modify the document structure you have 2 choices here. You can store your document as { fieldname:"nickname", value : "Howie" } { fieldname:"user/nickname", value: "Howie" } You can then query using db.so.find({fieldname:/nickname/, value:"Howie"}) Alternatively you can store as db.so.insert({value:"Howie", fieldpath:["nickname"]}) db.so.insert({value:"Howie", fieldpath:["user", "nickname"]}) The advantage with the second approach is that you can now index {fieldpath:1, value:1} and a query on it such as db.so.find({fieldpath:"nickname", value:"Howie"}) will be an indexed query.