Mongodb aggregation lookup with condition - mongodb

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(.....)

Related

Delete document matching condition in multi colection 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.

Update entire document in mongodb using morphia

I have document which I need to update but updated parameters are not same all the time it depends on user. So how can I update document or replace whole document with new values based on id.
You wrap your updated values in a Map the set the update:
Map<String, Object> updateInfo; // Key is db column name, value is updatedValue
Then create update operations:
Query<Entity> filter = datastore.createQuery(Entity.class)
.field("_id", id);
UpdateOperations<Entity> updateOps = datastore.createUpdateOperations(Entity.class);
updateInfo.entrySet().forEach(e -> updateOps.set(e.getKey(), e.getValue());
datastore.update(filter, updateOps);
By this way, you can update entity with any number of fields

Update Cloud Firestore Collection Based on "Master Collection"

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);
});
});

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.

Update an array using Jongo

I have a mongodb collection of the form
{
"_id":"id",
"userEmail":"userEmailFromCustomerCollection",
"customerFavs":[
"www.xyz.com",
"www.xyz.com",
"www.xyz.com"
]
}
I need to add an element to the customers favs array using Jongo , I am using the following code snippet to do so .
String query = "{userEmail:'"+emailId+"'}";
customerFavCollection.update(query).with("{$addToSet:{customerFavs:#}}", favUrl);
My problem , is that I need to upsert the document if the document does not
exist already , how can I do so using Jongo, I know an easier option would be to retrieve the document by Id and if it does not exist then insert the document using save() , but I am trying to avoid the extra retrieve.
You can add upsert() on the query.
customerFavCollection.update("userEmail:#", emailId)
.with("{$addToSet:{customerFavs:#}}", favUrl)
.upsert();