How to update multiple rows with different values in a single execution in mongodb? - mongodb

We know that in mysql instead of executing the below queries 3 times we can do the same in a single exection given in 1> and 2>
UPDATE feedbacks SET _id = '5c6a8bcfce1454086fefb879' WHERE user_rol = '26-02-2018';
UPDATE feedbacks SET _id = '5c6a89d3ce1454086fefb877' WHERE user_rol = '26-02-2017';
UPDATE feedbacks SET _id = '5c6a896ece1454086fefb876' WHERE user_rol = '26-02-2016';
1>
INSERT INTO feedbacks (_id, added_on)
VALUES
('5c6a8bcfce1454086fefb879', '26-02-2018'),
('5c6a89d3ce1454086fefb877', '26-02-2017'),
('5c6a896ece1454086fefb876', '26-02-2016')
ON DUPLICATE KEY UPDATE added_on = VALUES(added_on)
2>
UPDATE feedbacks
SET added_on = CASE
WHEN _id = '5c6a8bcfce1454086fefb879' THEN '26-02-2018'
WHEN _id = '5c6a89d3ce1454086fefb877' THEN '26-02-2017'
WHEN _id = '5c6a896ece1454086fefb876' THEN '26-02-2016'
END
WHERE _id IN ('5c6a8bcfce1454086fefb879', '5c6a89d3ce1454086fefb877', '5c6a896ece1454086fefb876')
Now my question is can we have any way to do the same (updating multiple rows with different values in a single execution) in mongodb?

In mongo with same condition or query, you can update multiple records by setting the 'multi' flag as true. Please refer link.
https://docs.mongodb.com/manual/reference/method/db.collection.update/
But, conditions are different this will not work.
For Bulk Update, You can refer:
https://docs.mongodb.com/manual/reference/method/Bulk.find.update/#Bulk.find.update

Related

java Mongo DB update field on condition

I am using spring-boot-starter-data-mongodb and I am performing an update Many to update several records in one operation
UpdateResult result = collection.updateMany(query, update);
The update field is defined like this
Bson update = Updates.combine(
Updates.set(field1, 1),
Updates.set(field2, 2);
But now I want to define a conditional update for field3. if field3 == null then field3=3
How can I achieve this? Thanks for your help

How to update multiple fields in two tables using inner join

I've two tables. And I need two fields to update.
they are connected to each other with a foreign key. I would like to update the fields found in these two tables by using my inner join.
UPDATE cert
SET cert.status = 1, doc.status = 1
FROM certificates cert
INNER JOIN documents doc ON doc.data_id = cert.certificate_id
WHERE cert.status = 0 AND cert.user_id = _expert_id AND doc.data_type = 'CERTIFICATE';
The code I mentioned above allows me to update the status field in the certificates table only. In addition, I would like to update the field in the documents table
Maybe this helps:
demo:db<>fiddle
WITH cert_update AS (
UPDATE certificates cert
SET status = 1
FROM documents doc
WHERE doc.data_id = cert.certificate_id
AND cert.status = 0
AND cert.user_id = '2' -- your "_expert_id"
AND doc.data_type = 'CERTIFICATE'
RETURNING certificate_id
)
UPDATE documents doc
SET status = 1
FROM cert_update cert
WHERE doc.data_id = cert.certificate_id
AND doc.data_type = 'CERTIFICATE';
Using the WITH clause (CTE) you are able to do several updates in one query. The first one gives back the updated certificate_id which can be used in the second query.

Yii2 Update many records with difference conditions

I want update many records with yii2 for insert use batchinsert() method and for update I don't know!!!
update my_tbl set fld1 = 321321 where res = 46513,
fld1 = 89876 where res = 54646,
fld1 = 64564 where res = 54654;
There is no bacthupdate() function in Yii2
Only batchinsert() is available
If you want you can iterate and update command or perform a sequence of single update like separate command.
Not also structured solution are actually available in Yii2

Getting a MongoDB document's field value into a variable

I am using mongo's shell and want to do what is basically equivalent to "SQL's select col INTO var" and then use the value of var to look up other rows in the same table or others (Joins). For example, in PL/SQL I will declare a variable called V_Dno. I also have a table called Emp(EID, Name, Sal, Dno). I can access the value of Dno for employee 100 as, "Select Dno into V_Dno from Emp where EID = 100). In MongoDB, when I find the needed employee (using its _id), I end up with a document and not a value (a field). In a sense, I get equivalent to the entire row in SQL and not just a column. I am doing the following to find the given emp:
VAR V_Dno = db.emp.find ({Eid : 100}, {Dno : 1});
The reason I want to do this to traverse from one document into the other using the value of a field. I know I can do it using the DBRef, but I wanted to see if I could tie documents together using this method.
Can someone please shed some light on this?
Thanks.
find returns a cursor that lets you iterate over the matching documents. In this case you'd want to use findOne instead as it directly returns the first matching doc, and then use dot notation to access the single field.
var V_Dno = db.emp.findOne({Eid : 100}, {Dno : 1}).Dno;
Using your query as a starting point:
var vdno = db.emp.findOne({Eid: 100, Dno :1})
This returns a document from the emp collection where the Eid = 100 and the Dno = 1. Now that I have this document in the vdno variable I can "join" it to another collection. Lets say you have a Department collection, a document in the department collection has a manual reference to the _id field in the emp collection. You can use the following to filter results from the department collection based on the value in your variable.
db.department.find({"employee._id":vdno._id})

Update columns based on subquery containing "select unnest" clause

I am attempting to update a boolean column in one table based upon the values in a second.
UPDATE channels
SET contains_photos = TRUE
WHERE id IN (SELECT unnest(ancestors)
FROM channel_tree WHERE id = 11329);
The channel_tree.ancestors column contains an array of channel IDs. The above is failing with the following error:
ERROR: cannot TRUNCATE "channel_tree" because it is being used by active queries in this session
The overriding goal is to set the contains_photos column to true for all ancestors of a given channel. Any one know how best to alleviate this error, or even an alternative solution?
No idea why your error says TRUNCATE. It sounds like you have a trigger or rule that is doing a truncate that we can't see.
Here's some alternative ways of doing that same query:
UPDATE channels
SET contains_photos = TRUE
WHERE id = ANY (SELECT ancestors
FROM channel_tree WHERE id = 11329);
Or with a join:
UPDATE channels
SET contains_photos = TRUE
FROM channel_tree
WHERE channels.id = ANY (channel_tree.ancestors)
AND channel_tree.id = 11329;