You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file holds connection to DB & also creates schema by importing them from other files.
//Import the mongoose module
const mongoose = require("mongoose");
//Import DB Models
const mymodel = require("./mymodel.js");
//Connect to DB , useMongoClient: true => Connect using new connection logic (old one deprecated)
//eg. URI : mongodb://localhost:port/dbname
mongoose.connect(" mongodb://localhost:port/dbname " , {
useMongoClient: true
}).then(()=>{
console.log("Successful connection to MongoDB");
})
.catch((err)=>{
console.log("Mongoose connection error due to: ",err);
});
/*
By default no mongoose promises have a .catch() (backwards compatibility reasons) (https://github.com/Automattic/mongoose/issues/3509)
So, we use global Promises
*/
mongoose.Promise = global.Promise;
//Expose the models for using elsewhere
module.exports = {
mymodel
}
Products = Table variable exported from mongoose.js
[projection]
Specifies what fields to return.
A projection cannot contain both include and exclude specifications, except for the exclusion of the _id field.
The projection parameter takes a document of the following form:
{ field1: <value>, field2: <value> ... }
The <value> can be any of the following:
- 1 or true to include the field in the return documents.
- 0 or false to exclude the field.
**The find() method always returns the _id field unless you specify _id: 0 to suppress the field.