crud operations posting to "test" database (Cosmosdb/Mongodb - Nextjs with mongoose)

rwkd26 21 Reputation points
2021-02-15T00:34:17.613+00:00

I am using Nextjs with mongoose to perform crud operations in my cosmosdb/mongodb database. But when i post or get data they are posted to a database called "test" and not the database i want to. I cant find a solution for changing the destination to where my data is posted. any help?

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,690 questions
{count} votes

Accepted answer
  1. Anurag Sharma 17,606 Reputation points
    2021-02-16T19:00:15.703+00:00

    Hi @@rwkd26 ,

    I was looking into the youtube video link you provided but could not get much out of it. Then there was this link which is having more insight.

    Instead of declaring the entire connection string in 'MONGO_URI' variable can you please create below variables. You can provide the database name which you want to use:

    COSMOSDB_USER = "<Azure Cosmos account's user name, usually the database account name>"  
    COSMOSDB_PASSWORD = "<Azure Cosmos account password, this is one of the keys specified in your account>"  
    COSMOSDB_DBNAME = "<Azure Cosmos database name>"  
    COSMOSDB_HOST= "<Azure Cosmos Host name>"  
    COSMOSDB_PORT=10255  
    

    Post this you can use the below code to connect:

    mongoose.connect("mongodb://"+process.env.COSMOSDB_HOST+":"+process.env.COSMOSDB_PORT+"/"+process.env.COSMOSDB_DBNAME+"?ssl=true&replicaSet=globaldb", {  
      auth: {  
        user: process.env.COSMOSDB_USER,  
        password: process.env.COSMOSDB_PASSWORD  
      },  
    useNewUrlParser: true,  
    useUnifiedTopology: true,  
    retryWrites: false  
    })  
    

    All this is mentioned in the same article I provided link for.

    Please let me know if this works, or else we can discuss more.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.