Z
Z
Zheka Dzecina2016-01-12 14:42:38
MongoDB
Zheka Dzecina, 2016-01-12 14:42:38

How to properly structure mongodb?

I am making a website where I will receive information from users about their place of residence. I am working on MeteorJS so the database is used by mongodb. How will the collection be structured correctly: create one object in which to make the structure Country -> City -> User? or just add each new user to the collection as a separate object? Or perhaps there is a better way to organize this? If there are (for example) a million users, it is necessary that the search be carried out as quickly as possible. There will be such requests in which it will be necessary to go through all users and find a field (for example, soap). Thanks

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
bromzh, 2016-01-12
@Zhe1ka

The best way is to take a relational database, since the structure and relationships are ripening here. For this they are needed.
When the questions “what structure to choose” or “how to store, everything in 1 document or by collections” begin, it immediately becomes clear that the choice of the database was wrong.
But since it’s a monga, it’s better to store everything in 1 document, and not spread everything over heaps of collections. And in order to quickly search (including by the fields of internal objects) there are indexes .
Yes, Mongi has both a semblance of transactions and joins. But in relational databases, this is all done better and more reliably, ACID, after all. So a minimum of collections, thicker documents.

E
E, 2016-01-12
@aylo

Meteor will initially give you the user's document schema (assuming you use the standard package from MDG Accounts)

{
  _id: "bbca5d6a-2156-41c4-89da-0329e8c99a4f",  // Meteor.userId()
  username: "cool_kid_13", // unique name
  emails: [
    // each email address can only belong to one user.
    { address: "[email protected]", verified: true },
    { address: "[email protected]", verified: false }
  ],
  createdAt: Wed Aug 21 2013 15:16:52 GMT-0700 (PDT),
  profile: {
    // The profile is writable by the user by default.
    name: "Joe Schmoe"
  },
  services: {
    facebook: {
      id: "709050", // facebook id
      accessToken: "AAACCgdX7G2...AbV9AZDZD"
    },
    resume: {
      loginTokens: [
        { token: "97e8c205-c7e4-47c9-9bea-8e2ccc0694cd",
          when: 1349761684048 }
      ]
    }
  }
}

And it seems to me it would be logical to push the data you need into profile , or create a location
object . It will not be difficult to get it later (arbitrary examples):
Meteor.user().profile.country
Meteor.user().profile.city

Meteor.user().location.country
Meteor.user().location.city

docs.meteor.com/#/full/accounts_api

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question