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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:09:03+00:00 2026-05-28T02:09:03+00:00

I have two collections, the first with an array of id’s referencing documents in

  • 0

I have two collections, the first with an array of id’s referencing documents in the second collection.

There is a particular place where I want to create a new DocumentType, then a new Document that references that DocumentType and finally update the original DocumentType to reference the new Document.

A DocumentType can have many Documents but a Document can only have a single DocumentType. The reason why I want to have the reference in both places is to simplify more complex queries later on.

My question is: is there a better way to write it using mongoose than to have these mulitple nested callbacks.

I’ve put together a simple example to demonstrate. There is more data in the real thing and as such I don’t want to have Documents embedded in DocumentTypes or visa versa.

var DocumentTypeSchema = new Schema({
    name:   String
  , desc:   String
  , documents:  [{type: Schema.ObjectId, ref: 'Document'}]
  , ...
});

var DocumentSchema = new Schema({
    title:    String
  , doctype:  {type: Schema.ObjectId, ref: 'DocumentType'}
  , ...
});

var Document = mongoose.model('Document', DocumentSchema);
var DocumentType = mongoose.model('DocumentType', DocumentTypeSchema);

// Begin the nested callbacks
// Create a document type
var type = new DocumentType({...});

// Save document type
type.save(function(err) {

  // Create new document
  var document = new Document({
      doctype: type.id
    , ...
  });

  // Save document
  document.save(function(err) {

    // Update document type
    DocumentType.update({_id: document.doctype}, {'$addToSet': {documents: document.id}}, function(err) {});
  });
});

Thanks for your help

  • 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-28T02:09:04+00:00Added an answer on May 28, 2026 at 2:09 am

    I would say that there generally is not a better way for synchronous processing. The series function of the async library mentioned by Jani is a fine way to flatten this, but I think it’s generally a good idea to stick with the callback structure – it makes the scoping very clear and reduces your external dependencies – unless the nesting gets to be absurd or you need async. Your nesting is not absurd, though you might consider async.parallel going forward. If you’re creating two new instances and saving attributes across both, there’s no reason to do the creation synchronously. Instead, something like this:

        async.parallel({
            type: function(callback) {
                var type = new DocumentType({...});
                type.save(function(err) {callback(null, type)});
            },
            document: function(callback) {
                var document = new Document({...});
                document.save(function(err) {callback(null, document)});
            }
        },
        function(err, results) {
            results.document.doctype = results.type.id;
            results.document.save();
            DocumentType.update(
              {_id: results.type},
              {'$addToSet': {documents: results.document.id}},
              function(err) {}
            );
        });
    

    It requires an extra save, and frankly may be silly for a task like this with so little performance overhead, but it illustrates a good approach for async processing and the versatility of the async library.

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

Sidebar

Related Questions

I have two Collections in a Java class.The first collection contains previous data, the
I have two Collection objects, I want to associate each object of these two
I have two set of integers(i.e. first one is: 2,3,4,5 and second one is
If i have two arrays First: A, B, C Second: ?, B, C I
I have two different collections: List<int> IEnumerable<IDataRecord> The first contains a list of primary
I have two collections created within Silverlight each collection is created at separate times
If I use unbounded wildcard types for two collections (each collection will have a
I have two collections which have property Email in both collections. I need to
I have a class which has two HashSet<String> collections as private members. Other classes
I have a Project Collection setup in my TFS2010RC deployment. I have two Projects

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.