Mongo DB - Update Document Field with GUID string using Mongosh - mongodb

We are trying to update a field in one of the collection. the Field name is "id". We just need to generate a UUID string and assign it to all documents within the collection. We tried with UUID() which is generating a BSON Object. But when we try to use toString() function, it is getting converted to some random symbols. How can we generate a new UUID string from Mongo Shell and update the field in a document?

new UUID().hex() returns the string without hyphens.
If you need the string with hyphens, you can use regex's match to slice and join.
const id = new UUID()
print(id.toString())
print(id.hex().match(/^(.{8})(.{4})(.{4})(.{4})(.{12})$/).slice(1,6).join('-'))

Related

Mongodb replace field type

I want to change a field type on mongodb for a collection from BsonBinary to ObjectId
I wonder what is the query I should use to convert that field ?
By mongodb shell
db.company.updateMany({}, {????})
I tried unset then set but still not working
Make a new field which type is ObjectId then fill it by the String field then delete String field for all and rename the new field to the String field name

How to change dataType text to ObjectId as a input Key in Mapreduce job

I want to process mongoDB ObjectId (_id) in mapReduce job and change the datattype ObjectId to String in result.
How can I change the deafult map function. Is there a way to convert type of objectid to string?

GraphLookup using the id property (string compared to ObjectId) using spring data mongo

Looking for options to use the graphlookup pipeline where the from field is the string value of the Object Id and the to field is Object Id.
You can make your 'from field' to be stored as ObjectId by adding following annotation:
#Field(targetType = FieldType.OBJECT_ID)
Still not possible. and there is no field casting available for $graphlookup

Why can't I query MongoDB id field with strings?

If I wish to access a document by id, and I happen to know the id is say: 557afc91c0b20703009f7ede, why do I need to use the ObjectId function to query the database, like so:
db.mydocs.find(ObjectId("557afc91c0b20703009f7ede"))
rather than the apparently more obvious:
db.mydocs.find("557afc91c0b20703009f7ede")
or:
db.mydocs.find({_id: "557afc91c0b20703009f7ede"})
?
From mongodb documentation:
ObjectId is a 12-byte BSON type
ObjectId is therefore not a string. What you are actually doing is building this 12-byte object from its string representation.
Because when one row is inserted in db then one id is generated in ObjectId format not in string with column name _id.

Convert string to Mongo ObjectID in Javascript (Meteor)

I have a Meteor application whereby I initially use the _id field from each record in my collection when naming list items in my template.
When get the _id field, I convert it to a string to use in the template.
Now I want to update these records in Mongo and am passing the _id back to a Meteor.method, but these are still in string format and Mongo is expecting an ObjectID(). Is there a simple way to convert this string to the ObjectID()? If not, what alternatives do I have?
Ok, found it! On the /server, within your Meteor method function do this to convert it:
var mid = new Mongo.ObjectID(str_id_sent_to_server);