Deprecation Warnings Using Node Driver

Note: this is almost exactly the same as NodeJS driver warnings: the options [xxxx] is not supported, but that was never answered, so I’m trying to ask it again.

What I’m trying to do is use the node.js driver to create a db class that can handle everything related to storing and getting data so that everything is a lot easier when I get to creating my backend. The way I’m doing this is by using methods that do various things. Currently I have a listDatabases method, a listCollections method, and a setDatabase method.
The listDatabases method connects then returns the admin.listDatabases(), and then disconnects.
The listCollections method connects, returns the db.collections() method and the disconnects.
The setDatabase method connects, sets a variable to the database object and returns the listCollections() method for the new database, and then disconnects.

I’m currently testing all of this by getting the databases, logging that, then setting the database and logging it.

The errors I’m getting are:

(node:43816) [MONGODB DRIVER] Warning: the options [servers] is not supported
(node:43816) [MONGODB DRIVER] Warning: the options [caseTranslate] is not supported 
(node:43816) [MONGODB DRIVER] Warning: the options [dbName] is not supported

and I’m getting these errors the second time I try to do something (in this case setting the database).
They don’t seem to do anything but I looked it up and it said that they could have unknown consequences, which kind of scares me.

One thing I found online is that this could be happening because my program is connecting again while it’s already connected. I’ve confirmed that this is not the case both using (too many) console.log’s, and making it so that it checks if it’s already connected with mongoClient.isConnected() before trying to connect again.

My full code (I’m using typescript):

import { Collection, Cursor, Db, MongoClient } from 'mongodb';
import { textChangeRangeIsUnchanged } from 'typescript';
import util from 'util';

interface result<type> {
	result: string;
	code: string;
	data?: type;
}

interface dbListOptions {
	getFullData?: boolean;
}
interface dbListData {
	databases: { name: string; sizeOnDisk: bigint; empty: boolean }[];
	totalSize: bigint;
	ok: bigint;
}

interface colListOptions {
	getFullData?: boolean;
}

class db {
	mongoUrl: string = 'mongodb://127.0.0.1:27017/?retryWrites=true&w=majority';

	mongoOptions: object = { useNewUrlParser: true, useUnifiedTopology: true };

	mongoClient: MongoClient = new MongoClient(
		this.mongoUrl,
		this.mongoOptions
	);

	cursorDatabase?: Db;

	cursorCollection?: Collection;

	cursor?: Cursor;

	constructor(database?: string, collection?: string) {
		if (database) {
			const client = this.mongoClient;
			client.connect().then(() => {
				this.cursorDatabase = client.db(database);
				if (collection) {
					this.cursorDatabase.collection(
						collection,
						{ strict: true },
						(err, col) => {
							this.cursorCollection = col;
							client.close();
						}
					);
				}
			});
		}
	}

	async connect() {
		if (!this.mongoClient.isConnected()) {
			console.log(this.mongoClient.isConnected())
			await this.mongoClient.connect();
			console.log("Connected")
		}
	}

	async close() {
		if (this.mongoClient.isConnected()) {
			console.log(this.mongoClient.isConnected())
			await this.mongoClient.close();
			console.log("Disconnected")
		}
	}

	async listDatabases(options: dbListOptions = {}) {
		const client = this.mongoClient;
		let result: result<any>;
		try {
			console.log("Pre-Pre-Connect")
			await this.connect();
			console.log("Post-Connect")
			const data: dbListData = await client
				.db('admin')
				.admin()
				.listDatabases();
			result = {
				result: 'SUCCESS',
				code: 'GOT DATABASES',
				data,
			};
		} catch (error) {
			return { result: 'ERROR', code: error };
		} finally {
			console.log("Pre-Pre-Disconnect")
			await this.close();
			console.log("Post-Disconnect")
		}
		result ||= { result: 'ERROR', code: 'UNKNOWN ERROR' };
		if (!options.getFullData && result.data) {
			if (!Array.isArray(result.data)) {
				result.data = Array.from(
					result.data.databases,
					(obj: { name: string }) => obj.name
				);
			}
		}
		return result;
	}

	get databases() {
		return this.listDatabases();
	}

	async setDatabase(dbName: string, options: colListOptions = {}) {
		const client = this.mongoClient;
		let result: result<Collection[]>;
		try {
			console.log("Pre-Pre-Connect")
			await this.connect();
			console.log("Post-Connect")
			this.cursorDatabase = client.db(dbName);
			const colListResult: result<Collection[]> =
				await this.listCollections();
			const data: Collection[] | undefined = await colListResult.data;
			result = data
				? {
					result: 'SUCCESS',
					code: 'SET DATABASE AND GOT COLLECTIONS',
					data,
				  }
				: {
					result: 'ERROR',
					code: 'COULD NOT GET COLLECTIONS',
				  };
		} catch (error) {
			return { result: 'ERROR', code: error };
		} finally {
			console.log("Pre-Pre-Disconnect")
			await this.close();
			console.log("Post-Disconnect")
		}
		result ||= { result: 'ERROR', code: 'UNKNOWN ERROR' };
		if (result.data?.length === 0) {
			result = { result: 'WARN', code: 'DATABASE EMPTY', data: [] };
		}
		return result;
	}

	async listCollections(options: colListOptions = {}, dbName?: string) {
		const client = this.mongoClient;
		let result: result<any>;
		try {
			const database: Db | undefined = dbName
				? client.db(dbName)
				: this.cursorDatabase;
			if (database) {
				console.log("Pre-Pre-Connect")
				await this.connect();
				console.log("Post-Connect")
				const data: Collection[] = await database.collections();
				result = {
					result: 'SUCCESS',
					code: 'GOT COLLECTIONS',
					data,
				};
			} else {
				result = { result: 'ERROR', code: 'NO DATABASE PROVIDED' };
			}
		} catch (error) {
			console.dir(error);
		} finally {
			console.log("Pre-Pre-Disconnect")
			await this.close();
			console.log("Post-Disconnect")
		}
		result ||= { result: 'ERROR', code: 'UNKNOWN ERROR' };
		if (!options.getFullData && result.data) {
			if (!Array.isArray(result.data)) {
				result.data = Array.from(
					result.data.databases,
					(obj: { name: string }) => obj.name
				);
			}
		}
		return result;
	}
}

export default db;
export { result, dbListData };

(async function () {
	const testdb = await new db();
	const databases: result<dbListData> = await testdb.listDatabases();
	console.log(util.inspect(databases, true, 3));
	const collections: result<Collection[]> = await testdb.setDatabase('storage');
	console.log(util.inspect(collections, true, 2));
})();

I believe re-assigning
this.mongoClient to

new MongoClient( this.mongoUrl, this.mongoOptions );
in this function

async close() { if (this.mongoClient.isConnected()) { console.log(this.mongoClient.isConnected()) await this.mongoClient.close(); console.log("Disconnected") } }This text will be hidden
is going to fix the problem. i’ve just ran into the same problem and figured it out.
let me know if it works.
edit: uhm, i’m new to the forum. could you tell me how you added a code block with this syntax? the [CODE] [//CODE] tags don’t seem to be a good choice here, they removed spaces for me.

Yes, from the testing I’ve done, that does seem like that is the solution.
The solution I found was basically the same; I just - instead of opening and closing on every operation - just open, do operations, and then close, creating new instances of the class when I need to. It should reduce cpu usage anyways.

(I just call the same open and close functions just in the usage, and not the class)

The way I added the code was that I clicked the little code icon
image
and then you just put 4 spaces before each line of code, ex:

this
is
some
code

(It should automatically detect and do highlighting)

EDIT:
Yup, I tested it and that works

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.