Help with understanding schema

When I first started experimenting with Mongo, I didn’t set up any formal kind of schema. I just imported data from a Google sheet. Frankly, I don’t remember if during the import it allowed for defining the schema during import.

I’ve used PHP in mysql since gasp the early 2000’s but I really wanted to modernize with mongo.

Well, there’s a bit of a learning curve. It’s not that difficult to understand how the database works.

That being said, I’m having a lot of issues with my queries. I think some of these issues are stemming around, not having a proper schema.

For example, I have a field I call date_added and I believe that is set as a string instead of date and I believe that’s causing a lot of issues with some of the queries I’m trying to make.

I’ve contemplated starting completely from scratch and rebuilding an entire new database exactly how it should have been created in Mongo….but I’d rather just change the database I currently have.

Could somebody please give me a step-by-step “best practice” approach on how to take a look at my current MongoDB and make the proper changes to the schema. For example, how do I change date_added from string to date?

It’s fairly simple to do for each individual document, but I’d rather make a mass change across the whole DB for the date_added field.

I have tried poking around in googling, but I can’t seem to find exactly what I’m looking for. It seems that every time I do a Google search I come up with somebody’s way to accomplish this by writing five or six lines of JavaScript and it just seems to me there’s gonna be a much easier way to accomplish such a simple thing.

Any suggestions? Thanks

Hi @Scott_Antonivich,

Welcome to the MongoDB Community! :wave:

I have read your post and you want to change your schema. MongoDB offers several resources and guides for best practices for schema designing. Here are,

The example problem that you’ve posted is how to update the field type, for that I can suggest you use the aggregation operations .

For your specific case: to update date_added field type from string to date, you can use $convert and $set aggregation operators in specific order stages and you can also verify field type by using $type.

MongoDB offers flexibility to perform aggregation operations on multiple documents consisting of a rich variety of operators. I suggest you to refer this documentation and you can smoothly manage your schema as well as the database :slight_smile:

Best,
Shreya

1 Like