Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A query to update a field in all documents with a unique id #5

Open
GIHAA opened this issue Mar 22, 2024 · 4 comments
Open

A query to update a field in all documents with a unique id #5

GIHAA opened this issue Mar 22, 2024 · 4 comments
Labels

Comments

@GIHAA
Copy link
Member

GIHAA commented Mar 22, 2024

db.collection.updateMany({},[{$set:{unique_id: {$function: {
                        body: function() {
                            return UUID().toString().split('"')[1];
                        },
                        args: [],
                        lang: "js"
                    }}}}])
@GIHAA GIHAA added the gist label Mar 22, 2024
@ThulinaWickramasinghe
Copy link
Collaborator

ThulinaWickramasinghe commented Apr 26, 2024

Can you look into this.

@SayuruRehan

@SayuruRehan
Copy link

UUID() is not defined in Mongo by default and instead of using split, we can do this. We can import the 'uuid' library in JS. Then the generated UUIDs are already returned as strings without double quotes therefore this split part will be unnecessary. We can use 'uuid.v4()'. Then this generated UUID can be assigned to the 'unique_id' field.

By removing the split, I believe we can reduce errors which can be caused by certain edge case scenarios as well.

This can be the updated code:

const uuid = require('uuid');
db.collection.updateMany({},[{$set:{unique_id: {$function: {
                        body: function() {
                            return uuid.v4();
                        },
                        args: [],
                        lang: "js"
                    }}}}])

I found these as well:
https://stackoverflow.com/questions/66688549/mongodb-update-all-the-documents-with-unique-id
https://www.npmjs.com/package/uuid

@ThulinaWickramasinghe
Copy link
Collaborator

ThulinaWickramasinghe commented Apr 28, 2024

@SayuruRehan Okay add the updated version. But first try it out yourself to see if it works.

Also mention the following in a convenient way.

This only works if security.javascriptEnabled is enabled which is a requirement and not preferable method. "For a mongos instance, see security.javascriptEnabled configuration option or the --noscripting command-line option starting in MongoDB 4.4."

@ThulinaWickramasinghe
Copy link
Collaborator

ThulinaWickramasinghe commented Apr 29, 2024

@SayuruRehan Please correct me if I'm wrong. The function we define in the body is not run on our backend server but in MondoDB. So I don't think this uuid.v4() is going to work even if we import it. However UUID function is implemented in MondoDB. Refer the link below.

https://www.mongodb.com/docs/manual/reference/method/UUID/

And also it's mentioned that MongoDB's js engine is based on SpiderMonkey from Mozilla instead of Chrome's V8 on which Node.js is built on. So I'm not sure even if somehow we imported uuid into the code from the node package you mentioned it's gonna work at all. Refer the first comment in the following question for more details.

https://stackoverflow.com/questions/58079309/where-is-specification-for-hex-md5-method-in-mongodb-shell

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants