Mongodb replace field type - mongodb

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

Related

MongoDb database Filter is not working in Compass

When I query all the records, i get the results as shown below.
When i copy an ID from the result and add it in the filter, no result is found.
Here is the example filter from documentation
What am i doing wrong? mongo db got to be kidding me :/
You are not providing the data type with the query. Since your _id field is ObjectId type and you are comparing as string that is why it is not working.
Change your filter condition and convert your string id to ObjectId type.
{_id: ObjectId('yourId')}
MongoDB provides an automatic unique identifier for the _id field in the form of an ObjectId data type.

Are custom _id fields in MongoDB still of type ObjectId

I'm building a project with MongoDB, but I'm a little bit confused on how the _id field works. My understanding is that, by default, MongoDB will generate an object of type ObjectId and assign it as the id.
However, in my server, I'm assigning an arbitrary string as the _id of an object. What I haven't quite understood yet: Is this string somehow converted to an ObjectId, or does the type of the _id field change to string?
MongoDB will generate an _id field with an ObjectId value if an _id field does not exist in the document. If you create the _id by your own, then you can use whatever datatype you like to use. MongoDB does not modify it.
The only constraint is, it must be a unique value.

Mongo DB - Update Document Field with GUID string using Mongosh

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('-'))

How to change a field type string to array in MongoDB?

I want to change datatype of collection without loosing any data. That datatype is inside array object.
Below is sample document where I want to change datatype of this field 'LiquidSiloNetworkId'

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);