CosmosDB migration MERN-Can't read collections

Dev, MERN apps 96 Reputation points
2020-11-06T05:06:38.903+00:00

I'm exporting a database from mlabs to CosmosDb.

after failing with other methods, I finally exported via mongodump and imported into CosmosDb. I can read the collections in studio 3t.

but when connecting in Express/mongoose via the connectionString, the collections all return [].

I've added my IP to the firewall.

It's not the code, when I point back to the mlab db everything retrieves correctly.

what am I missing in CosmosDb?

using:
"mongodb": "^3.0.4",
"mongoose": "^5.0.6",

router.get('/',function(req,res,next){
var ret={};
var query={"businessCode" : 'Travis'};//req.decoded.businessCode};
var cursor=db.collection('lookup').find(query).toArray(function(err,items){
if (err){
next (err);
} else {
//items = [] ...should definately return a collection
console.log ('****** items', items);
}
})
});

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,604 questions
0 comments No comments
{count} votes

Accepted answer
  1. Dev, MERN apps 96 Reputation points
    2020-11-06T19:21:57.423+00:00

    in azure, the generated connection strings don't quite match what the exercise sent by Kaylan sent. and I also wasn't setting some of the options. without the options I get a message "Password contains an illegal unescaped character"

    the Primary Connection string and Secondary Connection String add the following:
    &retrywrites=false
    &maxIdleTimeMS=120000
    &appName=@<appNam>@

    once I removed that from the connection string AND used the options, things worked.
    my final connection example:

        //pulled from config for clarity  
        const config = {'database': process.env.MONGODB_URI || 'mongodb://<user>:<password>@<db>.mongo.cosmos.azure.com:10255/<dbname>?ssl=true&replicaSet=globaldb'  
        }  
          
        mongoose.connect(config.database, //with  user and pass inside  
        	{  
        		useNewUrlParser: true,  
        		useUnifiedTopology: true,  
        		retryWrites: false,  
        		useFindAndModify: false,  
        		useCreateIndex: true  
        	})  
        	//.then(() => console.log('Connection to CosmosDB successful'))  
        	.catch((err) => console.error('CosmosDB error:', err))  
    
    
    
    
      
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. KalyanChanumolu-MSFT 8,321 Reputation points
    2020-11-06T12:08:36.62+00:00

    @Dev, MERN apps Welcome to Microsoft Q&A forums and thank you for your interest in Azure CosmosDB.

    We have a migration service to help customers migrate their existing MongoDB workloads to Azure. A step-by-step guide is here

    Since you have already migrated to CosmosDB, let us try to resolve the issue with your connection via mongoose.
    Are you getting any errors while establishing a connection? Please use the below code snippet to log any errors.

    mongoose.connect("mongodb://"+process.env.COSMOSDB_HOST+":"+process.env.COSMOSDB_PORT+"/"+process.env.COSMOSDB_DBNAME+"?ssl=true&replicaSet=globaldb", {  
      auth: {  
        user: process.env.COSMODDB_USER,  
        password: process.env.COSMOSDB_PASSWORD  
      },  
    useNewUrlParser: true,  
    useUnifiedTopology: true,  
    retryWrites: false  
    })  
    .then(() => console.log('Connection to CosmosDB successful'))  
    .catch((err) => console.error(err));  
    

    A detailed tutorial on connecting to CosmosDB via Mongoose is here
    Please try and let us know.


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.