k6pib6r6
k6pib6r6

Reputation: 119

Mongoose - operate with documents from an existing DB without knowing the exact schema

I have to connect to a mongo database with mongoose to preform a query. I have done similar before but in that case I defined the schema and the model so I made operations with the exported then required model in the application.

For this task I was provided with the mongoose connection's uri and the collections's name only and I have to make a node app and query the collection.

What is the way to query the collection with mongoose in this case?

Thanks for your help in advance.

Upvotes: 1

Views: 119

Answers (1)

mehta-rohan
mehta-rohan

Reputation: 1341

You must be good to go with below code.

var mongoose = require('mongoose'),
    Schema = mongoose.Schema;

mongoose.connect("uri");

var abcSchema = new Schema({
    strict: false
}, {
    collection: 'collectionName'
});
var abc = mongoose.model('collectionName', abcSchema);
abc.operations...

Upvotes: 1

Related Questions