HomeLearnHow-toDocument Enrichment and Schema Updates

Document Enrichment and Schema Updates

Updated: Aug 16, 2021 |

Published: Jul 22, 2021

  • Atlas
  • JavaScript
  • Aggregation Framework
  • ...

By Jesse Hall

Rate this article

So your business needs have changed and there’s additional data that needs to be stored within an existing dataset. Fear not! With MongoDB, this is no sweat.

In this article, I’ll show you how to quickly add and populate additional fields into an existing database collection.

#The Scenario

Let’s say you have a “Netflix” type application and you want to allow users to see which movies they have watched. We’ll use the sample_mflix database from the sample datasets available in a MongoDB Atlas cluster.

https://mongodb-devhub-cms.s3.us-west-1.amazonaws.com/I_Veb_N_Rm3_Ab3xi_F5c_Phpa3n_3zzf5pvv_I_Fsgnu3_X_6m_U_Knyev_Mrkvy_ZCTUF_5y_LM_Sv_WN_8_E11nbqzv3v_Mo95_C7_WYFGR_3l_OK_4_N2b_Rr9_Z4_Bu_U4_K_Cheipu_V_Rcvcq3_N_Wh_Zd3bu_J_Yafd_V_69c906938e

Here is the existing schema for the user collection in the sample_mflix database:

1{
2 _id: ObjectId(),
3 name: <string>,
4 email: <string>,
5 password: <string>
6}

#The Solution

There are a few ways we could go about this. Since MongoDB has a flexible data model, we can just add our new data into existing documents.

In this example, we are going to assume that we know the user ID. We’ll use updateOne and the $addToSet operator to add our new data.

1const { db } = await connectToDatabase();
2const collection = await db.collection(“users”).updateOne(
3 { _id: ObjectID(“59b99db9cfa9a34dcd7885bf”) },
4 {
5 $addToSet: {
6 moviesWatched: {
7 <movieId>,
8 <title>,
9 <poster>
10 }
11 }
12 }
13);

The $addToSet operator adds a value to an array avoiding duplicates. If the field referenced is not present in the document, $addToSet will create the array field and enter the specified value. If the value is already present in the field, $addToSet will do nothing.

Using $addToSet will prevent us from duplicating movies when they are watched multiple times.

#The Result

Now, when a user goes to their profile, they will see their watched movies.

https://mongodb-devhub-cms.s3.us-west-1.amazonaws.com/d_I_Tvq_WAL_4_Ap_Z_j_GN_Qx_Vu_Vxw6_3_Dt3_Krn_O_Fl_L9_OI_3be_DD_1_Q58dk_J39f7s0_Vn3p_Ptgcs_P_Ss3_XT_0_B_Kjcqh_H_Erw_J6if2_Ueeb_GA_Gc20_Fmfgt8j9g_H_Tm_Vow_Kk2_Hgb_Kbj_Eg9f_Xg_ACZ_Be_A_bb92f06e85

But what if the user has not watched any movies? The user will simply not have that field in their document.

I’m using Next.js for this application. I simply need to check to see if a user has watched any movies and display the appropriate information accordingly.

1{ moviesWatched
2 ? "Movies I've Watched"
3 : "I have not watched any movies yet :("
4}

https://mongodb-devhub-cms.s3.us-west-1.amazonaws.com/78ye_Oz_Njq_O_Rd2_Ngt_YQ_0ab_X_Fm_Vn_Eriu9_F_Bh_K8_Zx9_FD_Ls_ST_Uk_IR_Ee8_B_Iz_H_Tw_WI_Or_KB_Kzp_Ve_H_Eti_O66_fyb_MA_Eu_Yxa3gf2iq_Lg_Mska_O1w_W_Jc_Gk1_C7_Uk_F_Mud28_Sx_U4yse_0_Tb3_S9_F_e0d789b4bf

#Conclusion

Because of MongoDB’s flexible data model, we can have multiple schemas in one collection. This allows you to easily update data and fields in existing schemas.

If you would like to learn more about schema validation, take a look at the Schema Validation documentation.

I’d love to hear your feedback or questions. Let’s chat in the MongoDB Community.

Rate this article
MongoDB Icon
  • Developer Hub
  • Documentation
  • University
  • Community Forums

© MongoDB, Inc.