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 7957505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:15:34+00:00 2026-06-04T04:15:34+00:00

The example here http://mongoosejs.com/docs/populate.html Provides the following code var story1 = new Story({ title:

  • 0

The example here

http://mongoosejs.com/docs/populate.html

Provides the following code

var story1 = new Story({
      title: "A man who cooked Nintendo"
    , _creator: aaron._id
  });

_creator is defined above as follows

_creator : { type: Schema.ObjectId, ref: 'Person' }

If I modify the code to the following

  var story1 = new Story({
      title: "A man who cooked Nintendo"
    , _creator: {name: 'test'}
  });

It seems to happily insert the data into MongoDB.

{ "title" : "A man who cooked Nintendo", "_creator" : { "name" : "test" }, "_id" : ObjectId("4fb7a55315c5f2de07000002"), "fans" : [ ] }

How would I catch the error before insertion? I would like to check that it is not only an ObjectId but also that it corresponds to a valid Person.

  • 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-06-04T04:15:35+00:00Added an answer on June 4, 2026 at 4:15 am

    To continue what @JohnnyHK proposed, here is a complete solution(assuming _creator is a reference to a numeric id).

    If you want to check if the value is a valid ObjectId

    function isObjectId(n) {
      return mongoose.Types.ObjectId.isValid(n);
    }
    

    validate: [validator, ‘my error type’]

    _creator : { type: Schema.ObjectId, ref: 'Person', validate: isObjectId }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to adapt the example here http://mongoosejs.com/docs/populate.html I have removed stories and am
Following the example here http://www.java2s.com/Code/JavaAPI/javax.swing/JOptionPaneshowInputDialogComponentpObjectmStringtintmIconiObjectoObjecti.htm is it possible to somehow make the default jcombobox
Am attempting to model my code after the example here: http://connect.soundcloud.com/examples/connecting.html# It works in
here's a portion of my code (taken from google charts example here http://code.google.com/apis/chart/image/docs/post_requests.html )
I'm following the example here: http://developer.android.com/resources/tutorials/views/hello-tabwidget.html Everything works fine. The first time I click
I'm following the example here: http://code.google.com/p/modwsgi/wiki/IntegrationWithPylons however, it doesn't work - I get ImportError:
Hi I have a code example available here: http://jsbin.com/oxoweh My problem is that I
Suppose we have this example: http://techdroid.kbeanie.com/2009/07/custom-listview-for-android.html with source code available here: http://code.google.com/p/myandroidwidgets/source/browse/trunk/Phonebook/src/com/abeanie/ How can
I am using the jQuery spinner tool found here: http://btburnett.com/spinner/example/example.html When I try using
Take a look at the example here: http://www.brianhare.com/physics/so.html Take a look at console.log where

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.