简体   繁体   中英

Mongodb - I have 2 collections `Users` and `Markers` - How do I handle adding the `userid` to new Marker created?

I just have 2 collections right now - Users and Markers .

Users schema is something like this:

{
  username: String,
  isVerified: Boolean,
  zipCode: Number
}

Each new user that I create in Users has an objectId like this:

{
 "_id" : ObjectId("588fbd3e39b266783285d573"), 
 "username" : "testuser",
 "isVerified": true,
 "zipCode": 12345
}

Markers is a collection of Marker objects created by users. It has some title, description, latLng, etc. However, I want to add the Users.username and also a userid of some kind. For this purpose, is it fine to just use the Users._id ?

I'm fairly new to nosql database design so please bear with me. Since I am returning Markers data to the client side, I need a userid so I can open a user's profile page from it since my API can just use a userId parameter. But, should I have both username and userid in each Marker?

** after some reading I've decided to switch to postgres and learn that since my data is relational. thx for the help

Assuming that your username is unique, you can either have username or userid in your Markers connection. Mongodb does not support foreign keys intrinsically, nor does it support referential integrity. So it will have to be up to you update/delete these references as the data changes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM