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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:55:11+00:00 2026-06-14T12:55:11+00:00

I need to insert some data using mongoose but the name of the collection

  • 0

I need to insert some data using mongoose but the name of the collection is provided by the user at the moment of the insertion, so I first have to check if the collection exists.

The way I know how to check if a collection exists is by querying the system.namespaces collection. I can see 3 possible approaches to doing that.

  1. Find a way to query system.namespaces using mongoose (maybe defining a schema that matches the one in the db).
  2. Getting some underlying node-mongodb-native object from mongoose and performing the query manually. In any case, this is something I would like to learn how to do.
  3. Using a separate instance of a node-mongodb-native (or some other driver) to perform the query

Number 3 is the least elegant and the one i’m trying to avoid, I don’t want to load another instance of the driver nor create a new connection when mongoose already created one.

I’m going to try number 1 after writing this. I just checked system.namespaces and the schema looks quite simple

I’d still like to hear some opinions.

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-06-14T12:55:12+00:00Added an answer on June 14, 2026 at 12:55 pm

    Option 2 is probably the cleanest. Assuming you have a Mongoose Connection object named conn that’s been opened using mongoose.createConnection, you can access the native mongo Db object via conn.db. From there you can call collectionNames which should provide what you’re looking for:

    conn.db.collectionNames(function (err, names) {
        // names contains an array of objects that contain the collection names
    });
    

    You can also pass a collection name as a parameter to collectionNames to filter the results to just what you’re looking for.

    Mongoose 4.x Update

    In the 2.x version of the MongoDB native driver that Mongoose 4.x uses, collectionNames has been replaced by listCollections which accepts a filter and returns a cursor so you would do this as:

    mongoose.connection.db.listCollections({name: 'mycollectionname'})
        .next(function(err, collinfo) {
            if (collinfo) {
                // The collection exists
            }
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i need to insert some data in a raw. suppose first i insert username,
I need to write some code to insert around 3 million rows of data.
I have a ModelAdmin where I need to insert some html-snippet that is not
I need to insert Chinese characters inside Oracle Data base using Java (hibernate 3.0
I need to copy some data from one table to another but I need
I have a page where a user can insert some order about some product.
I need to push some data from and old application using access to the
Need to insert selected text on the page into textarea. There must be some
I need some help to insert comma separates strings into multiple columns into db.
Hi need insert data in table if the record is not already exits Ex:

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.