0

I am using MongoDB for the first time, and I have some experience with NoSQL databases.

I am attempting to replicate behaviour that I have managed to achieve on Google's Cloud Firestore: enter image description here

I want to create a collection within a document. I have not been able to replicate this behaviour using MongoDB as I cannot find code in the documentation. Is this behaviour even possible please?

Thanks in advance.

Edit: Here is a screenshot of a sample document in biometric_data : enter image description here

Mar
  • 106
  • 1
  • 9
  • 1
    Can you also share a screenshot of what the documents in `biometric_data` contain? Also this might be useful: [Is mongodb sub documents equivalent to Firestore subcollections?](https://stackoverflow.com/q/68574830/13130697) – Dharmaraj Dec 27 '21 at 11:42
  • Sure, they are simply documents containing preferred units selected by the users (this is a prototype and more values will be added later on) – Mar Dec 27 '21 at 11:48
  • try this :https://docs.mongodb.com/manual/tutorial/insert-documents/ – N MZ Dec 27 '21 at 12:13

1 Answers1

1

MongoDB has embedded documents which can be used to store the same data. You can try creating an array of sub-documents (each having name and data property):

{
  name: "",
  email: "",
  ...otherFields,
  biometric_data: [
    {
      name: "glucose",
      data: {
        preferred_unit: "mg/dL"
        // Add new properties as required
      }  
    },
    {
      name: "weight",
      data: {
        preferred_unit: "KG"
      }  
    }
  ],
  ...templateData
}

However, a document's size in MongoDB cannot exceed 16 MB. If number of fields in biometric_data are limited then you can use sub-documents otherwise you might have to create another collection to store those as documents (generally preferred for chat apps or where number of sub-documents can be really high).


Sub-collections (in Firestore) allow you to structure data hierarchically, making data easier to access. For example, users and posts collections can be structured in either of the ways below:

  • With sub-collection
users -> {userId} -> posts -> {postId}
  • Root level collections
users -> {userId}

posts -> {postId}

Though if you use root level collections, you must add a userId in posts document to identify who the owner of a post is.

If you use nested documents way in MongoDB, you are likely to hit the 16 MB document limit if any of the users decides to add many posts. Similarly if the biometric_data array can have many documents, it'll be best to create another collection.

Firestore's sub-collections and documents do not count towards 1 MB max doc size of parent document but nested documents in MongoDB do.

Also checkout:

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
  • So as a solution, simply creating an array of Objects would be the equivalent? – Mar Dec 27 '21 at 12:22
  • @Mar yes, thats an array of embedded documents where each document has type `{name: "", data: {}}`. Do checkout the linked page to MongoDB's documentation, they have examples showing how to query nested documents – Dharmaraj Dec 27 '21 at 12:24
  • This isn't particularly what I'm looking for, but if this is the only solution available I'll use this. Thanks for your help – Mar Dec 27 '21 at 13:43
  • @Mar MongoDB does not have Firestore like sub-collections concepts and Firebase console like UI where you can view it that way if that's what you are looking for. The schema explained in my answer would be the closest way to it and you can still perform all necessary operations. – Dharmaraj Dec 27 '21 at 13:45