Friday, 8 May 2020

Help! How to use Mongoose to connect a pre-existing database?

New to Node and Mongo.I basically want to querymongodb+srv://readonly:readonly@covid-19.hip2i.mongodb.net/covid19 this database hosted by mongodb. There is code available using "mongodb" module which looks likeconst MongoClient = require('mongodb').MongoClient; const assert = require('assert'); const filter = { 'country': 'China' }; MongoClient.connect( 'mongodb+srv://readonly:readonly@covid-19.hip2i.mongodb.net/covid19?authSource=admin&replicaSet=covid-19-shard-0&readPreference=primary&appname=MongoDB%20Compass%20Community&ssl=true', { useNewUrlParser: true, useUnifiedTopology: true }, function(connectErr, client) { assert.equal(null, connectErr); const coll = client.db('covid19').collection('global'); coll.find(filter, (cmdErr, result) => { assert.equal(null, cmdErr); }); client.close(); }); But I want to use mongoose. It should be possible but I cannot find any resource which tells me how to select a database and a collection then to filter using mongoose.TLDR: All I want is to do a simple query on mentioned database, above is the code using mongodb module, Is it possible to do using mongoose?

Submitted May 08, 2020 at 12:32PM by shekhar567

No comments:

Post a Comment