DEV_JUNGLE
(Dev Jungle)
September 26, 2023, 7:39am
1
{
"_id": 1,
"skills": [
{
"uid": 1000,
"grade": 1,
"count": 1
},
{
"uid": 2000,
"grade": 2,
"count": 1
},
{
"uid": 3000,
"grade": 10,
"count": 2
}
]
}
This is my data. I want to change “count” of two uid (1000,3000) count of uid=1000 is 777, and count of uid = 2000 is 888. how to change at once.
db.nonames.updateOne(
{_id:1},
{ $set : {"skills.$[elem].count" : [100,200] }},
{ arrayFilters :[ {"elem.uid":{"$in":[1000,3000]}} ]}
)
My query is not good and not working.
steevej
(Steeve Juneau)
September 27, 2023, 12:39pm
3
If you want to modify more than a single element, you simply need multiple arrayFilters, one for each element you want to modify. If I understand your situation correctly you may try the following untested code to see if it works.
uid_1 = 1000
count_1 = 777
uid_2 = 2000
count_2 = 888
db.nonames.updateOne(
{ "_id" : 1 } ,
{ "$set" : {
"skills.$[one].count" : count_1 ,
"skills.$[two].count" : count_2
} } ,
{ "arrayFilters" : [
{ "one.uid" : uid_1 } ,
{ "two.uid" : uid_2 }
] }
) ;
While at it, could you please provide followup and potential mark as solve your thread
The easiest way is just convert the CSV to JSON Array.
To convert a CSV file to JSON format, you can use a CSV parsing library such as csv-parser in Node.js or the csv library in Python. Here’s an example in Node.js:
First, install the csv-parser package using npm:
npm install csv-parser
Next, use the csv-parser package to parse the CSV file and convert it to JSON format:
const fs = require('fs');
const csv = require('csv-parser');
const results = [];
fs.createReadStream('path/to/csv/fil…
2 Likes
system
(system)
Closed
November 26, 2023, 12:40pm
4
This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.