MongoDB randomly fail to drop sharded database - node.js

I have few integration tests that use real MongoDB database version 3.2. Tests are run with Mocha and I'm dropping the test db after each test with following code. It uses Mongoose version 4.4.12 and native MongoDB Node.js driver.
afterEach((done) => {
const connection = mongoose.createConnection(config.db.host);
connection.once("open", () => {
connection.db.command({ dropDatabase: 1 })
.then((res) => done(null, res))
.catch(done);
});
})
It worked nicely until I enabled sharding in my local environment to cover that with tests as well. Dropping database started to fail randomly. Above code fails to drop my test db about on every third test run. My sharding setup is minimal and have one config server, one mongos and two mongo shards.
After I added some logging I noticed that connection.db.command({ dropDatabase: 1 }) returns { info: 'database does not exist', ok: 1 } on those failed runs. Database is not dropped because MongoDB thinks it is already dropped even it is not the case.
Am I doing it completely wrong? Is there some Mongoose / MondoDB Node.js native driver configuration or such that I'm missing here? Or is this some known problem? Any help is much appreciated.
EDIT: Same thing happens if I use MongoClient directly, so Mongoose is not causing this.

Related

mongodb db not showing up

I am trying to use MongoDB for a REST API project. Yesterday, the db worked fine and it showed up in command prompt. Now, when I run db in my mongo terminal, my database does not show up. It even says that it connected to the MongoDB when I run my server which uses this code:
const connectWithRetry = () => {
console.log('MongoDB connection with retry')
mongoose.connect("mongodb://localhost:27017/rest-tutorial", options).then(()=>{
console.log('MongoDB is connected')
}).catch(err=>{
console.log('MongoDB connection unsuccessful, retry after 5 seconds. ', ++count);
setTimeout(connectWithRetry, 5000)
})
};
I'm new to this so I am kind of unsure what to do. The code works fine and when I register new users and send data to the database, I get my desired output. However, I am unable to see the stored data in command line. What can I do?
EDIT AS REQUESTED
mongo commands run:
db
test
show collections
(nothing shows up even though I submitted data. This is also the wrong db)

Test using mongodb-memory-server is failing/timing out

I’m setting up mongodb-memory-server in my backend for test purposes and am experiencing some issues when running tests that I need to debug. My issue is that when I run my test (which will create a mongodb doc somewhere in the service being tested), the test times out.
As I understand it, this is because when the test is executed and a new mongo doc is trying to be created during the test, I console log mongoose.connection.readyState and it says it’s 0, meaning that mongoose is disconnected. This is strange to me because I added console logs to my connectMongoose() function (pictured below) and it says that mongoose is connected.
So my main question is why does it say mongoose is connected at the end of connectMongoose(), but it says it’s disconnected during the execution of the unit test/service function? How can I ensure that MongoDB-memory-server is fully connected prior to test execution?
Below is a screenshot showing how I am doing the mongoose test connection:
Below this is a screenshot of exactly where and how mongodb-memory-server is being used:
Here is a screenshot of my jest.config.js:
And finally the actual test file which has the failing test (what I’m asking about):
beforeAll(connectMongoose)
beforeEach(clearDatabase)
afterAll(disconnectMongoose)
Your three functions here are async functions, but you don't await them - is it possible that the connect Mongoose returns whilst the promise is still awaiting, and the other code continues despite the async function not having completed yet?
Perhaps this would better serve your purpose?
beforeAll(() => {
await connectMongoose
})
Before :
beforeAll(connectMongoose)
beforeEach(clearDatabase)
afterAll(disconnectMongoose)
After:
beforeAll(async() => {await connectMongoose})
beforeEach(async() => {await clearDatabase})
afterAll(async () => { await disconnectMongoose})
The reason is you should wait until the mongoose connection is done completely and remove
set timeout in connectMongoose function not needed there.If you want to use jest timeout you can use it in the beforeEach function.

How to check if Redis in nodejs app is working

I installed redis and mongodb in VM, I'm trying to speed the response time of my mongoose find() requests .. I used lean() and it's working good .. then I found redis and I installed it and I followed this tutorial to set up redis in my controllers :
https://epsagon.com/development/using-redis-to-optimize-mongodb-queries/
I created the cache.js in /services with connection details and then in my controller code I imported :
const { clearKey } = require("../services/cache");
And in my find() I added .cache() :
await Book.find({ author: req.query.author }).cache();
I want to know how the app is knowing the .cache() function is my redis server because I only imported "clearKey " and I didn't use it ? And the performance is not speeded so I don't know if the Redis set up is correct and working or not. How to check that ?
Thanks
You added the cache function to mongoose.Query.prototype.cache, it means that you extended mongoose.Query with cache function and it was automatically added to all Mongoose models.
Regarding your second question, you need to add to your Redis:
const client = redis.createClient({
host: keys.redisHost, // make sure its your redis host
port: keys.redisPort, // make sure its your redis port
retry_strategy: () => 1000
});
When your query is running over MongoDB you should see a log:
console.log("Response from MongoDB");
And when your query is running over Redis you should see a different log:
console.log("Response from Redis");
You can see more the this github file.

How to make connection with 2 DBs while using mongo official driver for node js

Am trying to make the connection from node js to Mongo DB.
while using mongo's official node js driver Official
I am able to make a connection with DB at one at a time.
but am looking for a way to get connected with 2 DBs at a time.
for a fallback issue.
Approach one:
const MONGO_CLIENT = require('mongodb').MongoClient;
const MONGO_URL = mongodb://USER:PASS#HOST:PORT/DBNAME
MONGO_CLIENT.connect(MONGO_URL, (err: any, db: any) => {
if (err) {
res.status(503).send(err);
}
console.log ('Connected do the things');
}
I got success by this way.
but am looking for a fallback way.
while setting two Hosts in URL I got errors
const MONGO_URL = mongodb://USER:PASS#PRIMARY_HOST:PRIMARY_HOST_PORT,SECONDARY_HOST:SECONDARY_HOST_PORT/DBNAME
am getting following errors
{
"name":"MongoError",
"message":"seed list contains no mongos proxies, replicaset connections requires the parameter replicaSet to be supplied in the URI or options object, mongodb://server:port/db?replicaSet=name"
}
Could anyone please explain to me what am doing wrong. and how should I connect with multiple DBS?
NOTE: Primary and secondary DBs are same and always synched.

Mongoose calls hangs

I haven't worked on my PC for few days.
Suddenly all the calls to mongo via mongoose hangs up, the callbacks are not called.
I checked that my call to .connect works, and that the connection state is 1 (connected).
I also made sure mongo service is running on localhost and the appropriate port 27017, and I can use the mongo console and query the db manually.
I also scanned the Internet for solutions but all I found was 'check that you're actually connected', and I verified that already.
Mongoose version 2.15.0, mongo version 2.4.9 and node js version is 4.4.2.
I fixed it.
Problem was duplicate references to the mongoose module.
I had a mongoose reference locally (which was connected), but my schema was present higher in the node_modules hierarchy, and it have used another mongoose instance which had no connection.
Once I removed the duplicate mongoose modules (npm uninstall mongoose one of them) it worked.
Above solutions didn't work for me so I fixed with following solution.
I had same issue where my db calls used to hang with no invocation to my callbacks or the promise resolution.
The problem was I used "createConnection()" to establish the connection with the db. But it didn't work perfectly.
Instead using "connect()" and "connection" imports worked.
Here is the sample code. Hope this helps.
import { connect, connection } from "mongoose";
const mongoUri = `mongodb://${my_mongo_host_&_port}`;
connect(mongoUri, {}); //to connect to my standalone db
//"connection" to listen to events
connection.on("connected", () => {
console.log("MongoDB connection established!", mongoUri);
});
I am working with "mongoose": "^6.6.1".

Resources