JavaScript
x
2
1
{'some': 'abcdefg', 'some2': 'hijklmn'}
2
Desired output
JavaScript
1
2
1
{'some': 'abc', 'some2': 'hij'}
2
in other words substring [:3]
How can I achieve this with pymongo
Advertisement
Answer
You can use updatemany with an aggregation : https://docs.mongodb.com/manual/tutorial/update-documents-with-aggregation-pipeline/
JavaScript
1
15
15
1
update_pipe = [
2
{ "$set":
3
{ "some":
4
{ "$substr": ['$some', 0,3]}
5
},
6
},
7
8
{ "$set":
9
{ "some2":
10
{ "$substr": ['$some2', 0,3]}
11
},
12
}
13
]
14
collection.update_many({}, update_pipe)
15