Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6995937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:05:37+00:00 2026-05-27T20:05:37+00:00

Below is my Schema. var UserModel = new Schema({ id: ObjectId , email: String

  • 0

Below is my Schema.

var UserModel = new Schema({
    id: ObjectId
  , email: String
  , firstName: String
  , lastName: String
  , password: String
});

var MessageModel = new Schema({
    id: ObjectId
  , createdDate: { type: Date, default: Date.now }
  , user: String // should this be [UserModel]?
  , body: String
});

For my case, every message has a user but only one. Should I Embed UserModel or should I leave the user field as a string. One future use case would be to return a query that has the body of the message, creation date, and user (first name and last name concatenated). Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:05:38+00:00Added an answer on May 27, 2026 at 8:05 pm

    Short answer: No, you should not use the UserModel as a subdocument of MessgeModel.

    Long answer: First, reconsider your naming. You are actually defining schemas here. Later, you will be associating a model with each of these schemas. So, UserSchema and MessageSchema would be more appropriate here.

    But that’s not germane. Regarding your question, you’re MessageModel schema should not contain embedded documents representing users unless there is a 1-to-1 relationship. However, I expect that each user will be associated with many messages (hopefully). So, you don’t want a new copy of the user (each with a new _id) for every message he creates. You only want one canonical document for each user, and a reference to that user in the MessageModel.

    Now, using a string reference may be the right choice for you. But if you anticipate running a query on the MessageModel in which you’d like the user attribute to be populated by the actual UserModel document, then you’ll want to use a ref.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the schema below - with a query node as the context, is it
I need to create program that creates a XML schema like below using System.Xml.XmlSchema
Below is my (simplified) schema (in MySQL ver. 5.0.51b) and my strategy for updating
My spring-context file is shown below. <beans xmlns=http://www.springframework.org/schema/beans xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xmlns:aop=http://www.springframework.org/schema/aop xmlns:jee=http://www.springframework.org/schema/jee xmlns:jms=http://www.springframework.org/schema/jms xmlns:lang=http://www.springframework.org/schema/lang xmlns:tx=http://www.springframework.org/schema/tx
Basically I've got the below schema and I'm inserting records if they don't exists.
Below is schema description. I would like to construct a query that for a
I have defined a xml schema as below <?xml version=1.0 encoding=utf-8?> <xs:schema id=PacketTemplate targetNamespace=http://tempuri.org/PacketTemplate.xsd
I have the schema below CREATE TABLE `vocabulary` ( `vid` int(10) unsigned NOT NULL
I have a many-to-many relation with a linked table. See (simplified) schema below. Created
The assembly qualified string used as a parameter below for a Uri works in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.