The Model.prototype.base property of the Mongoose API is used to return the base Mongoose instance the model uses.
Syntax:
Model_Name.base
Return Value: The Model.prototype.base property returns the base Mongoose instance the model uses.
Setting up Node.js application:
Step 1: Create a Node.js application using the following command:
npm init
Step 2: After creating the NodeJS application, Install the required module using the following command:
npm install mongoose
Project Structure: The project structure will look like this:
Example 1: In this example, we have established a database connection using mongoose and defined model over userSchema, having two columns or fields ânameâ and âageâ. In the end, we are using the base property on the User model which will return the base instance used by the model.
- app.js: Write down the below code in the app.js file:
// Require mongoose module
const mongoose = require('mongoose');
// Set Up the Database connection
mongoose.connect(
'mongodb://localhost:27017/geeksforgeeks', {
useNewUrlParser: true,
useUnifiedTopology: true
})
const userSchema = new mongoose.Schema(
{ name: String, age: Number }
)
// Defining userSchema model
const User = mongoose.model('User', userSchema);
const output = User.base
console.log("Base is:",output)
Steps to run the program: To run the application execute the below command from the root directory of the project:
node app.js
Output:
Base is: <ref *1> Mongoose {
connections: [
NativeConnection {
base: [Circular *1],
collections: [Object],
models: [Object],
config: {},
replica: false,
options: null,
otherDbs: [],
relatedDbs: {},
states: [Object: null prototype],
_readyState: 2,
_closeCalled: false,
_hasOpened: false,
plugins: [],
id: 0,
_queue: [],
_listening: false,
_connectionString: 'mongodb://localhost:27017/geeksforgeeks',
_connectionOptions: [Object],
client: [MongoClient],
'$initialConnection': [Promise]
}
],
models: { User: Model { User } },
events: EventEmitter {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
[Symbol(kCapture)]: false
},
__driver: {
Binary: [Function: Binary] {
fromExtendedJSON: [Function (anonymous)],
BSON_BINARY_SUBTYPE_DEFAULT: 0,
BUFFER_SIZE: 256,
SUBTYPE_DEFAULT: 0,
SUBTYPE_FUNCTION: 1,
SUBTYPE_BYTE_ARRAY: 2,
SUBTYPE_UUID_OLD: 3,
SUBTYPE_UUID: 4,
SUBTYPE_MD5: 5,
SUBTYPE_ENCRYPTED: 6,
SUBTYPE_COLUMN: 7,
SUBTYPE_USER_DEFINED: 128
},
Collection: [Function: NativeCollection],
Decimal128: [Function: Decimal128] {
fromString: [Function (anonymous)],
fromExtendedJSON: [Function (anonymous)]
},
ObjectId: [Function: ObjectId] {
getInc: [Function (anonymous)],
generate: [Function (anonymous)],
createPk: [Function (anonymous)],
createFromTime: [Function (anonymous)],
createFromHexString: [Function (anonymous)],
isValid: [Function (anonymous)],
fromExtendedJSON: [Function (anonymous)],
index: 15789560
},
ReadPreference: [Function: readPref],
getConnection: [Function (anonymous)]
},
options: {
pluralization: true,
autoIndex: true,
autoCreate: true,
[Symbol(mongoose:default)]: true
},
_pluralize: [Function: pluralize],
Schema: [Function: Schema] {
reserved: [Object: null prototype] {
validate: 1,
toObject: 1,
save: 1,
remove: 1,
populated: 1,
isNew: 1,
isModified: 1,
init: 1,
get: 1,
errors: 1,
collection: 1,
removeListener: 1,
listeners: 1,
emit: 1,
prototype: 1
},
Types: {
String: [Function],
Number: [Function],
Boolean: [Function],
DocumentArray: [Function],
Subdocument: [Function],
Array: [Function],
Buffer: [Function],
Date: [Function],
ObjectId: [Function],
Mixed: [Function],
Decimal: [Function],
Decimal128: [Function],
Map: [Function],
Oid: [Function],
Object: [Function],
Bool: [Function],
ObjectID: [Function]
},
ObjectId: [Function: ObjectId] {
schemaName: 'ObjectId',
defaultOptions: {},
get: [Function (anonymous)],
set: [Function: set],
_checkRequired: [Function (anonymous)],
_cast: [Function: castObjectId],
cast: [Function: cast],
_defaultCaster: [Function (anonymous)],
checkRequired: [Function (anonymous)]
}
},
model: [Function (anonymous)],
plugins: [
[ [Function (anonymous)], [Object] ],
[ [Function (anonymous)], [Object] ],
[ [Function], [Object] ],
[ [Function (anonymous)], [Object] ],
[ [Function: trackTransaction], [Object] ]
],
default: [Circular *1],
mongoose: [Circular *1]
Example 2: In this example, we have established a database connection using mongoose and defined model over studentSchema, having three columns or fields ânameâ, âfather_nameâ, and âmother_nameâ. In the end, we are using base property on the Student model which will return base instance of the model.
- app.js: Write down the below code in the app.js file:
// Require mongoose module
const mongoose = require('mongoose');
// Set Up the Database connection
mongoose.connect(
'mongodb://localhost:27017/geeksforgeeks', {
useNewUrlParser: true,
useUnifiedTopology: true
})
const studentSchema = new mongoose.Schema({
name: String,
father_name: String,
mother_name: String
})
// Defining studentSchema model
const Student = mongoose.model('Student', studentSchema);
// Accessing base property on Student model
const output = Student.base
console.log("Base is:", output)
Steps to run the program: To run the application execute the below command from the root directory of the project:
node app.js
Output:
Base is: <ref *1> Mongoose {
connections: [
NativeConnection {
base: [Circular *1],
collections: [Object],
models: [Object],
config: {},
replica: false,
options: null,
otherDbs: [],
relatedDbs: {},
states: [Object: null prototype],
_readyState: 2,
_closeCalled: false,
_hasOpened: false,
plugins: [],
id: 0,
_queue: [],
_listening: false,
_connectionString: 'mongodb://localhost:27017/geeksforgeeks',
_connectionOptions: [Object],
client: [MongoClient],
'$initialConnection': [Promise]
}
],
models: { Student: Model { Student } },
events: EventEmitter {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
[Symbol(kCapture)]: false
},
__driver: {
Binary: [Function: Binary] {
fromExtendedJSON: [Function (anonymous)],
BSON_BINARY_SUBTYPE_DEFAULT: 0,
BUFFER_SIZE: 256,
SUBTYPE_DEFAULT: 0,
SUBTYPE_FUNCTION: 1,
SUBTYPE_BYTE_ARRAY: 2,
SUBTYPE_UUID_OLD: 3,
SUBTYPE_UUID: 4,
SUBTYPE_MD5: 5,
SUBTYPE_ENCRYPTED: 6,
SUBTYPE_COLUMN: 7,
SUBTYPE_USER_DEFINED: 128
},
Collection: [Function: NativeCollection],
Decimal128: [Function: Decimal128] {
fromString: [Function (anonymous)],
fromExtendedJSON: [Function (anonymous)]
},
ObjectId: [Function: ObjectId] {
getInc: [Function (anonymous)],
generate: [Function (anonymous)],
createPk: [Function (anonymous)],
createFromTime: [Function (anonymous)],
createFromHexString: [Function (anonymous)],
isValid: [Function (anonymous)],
fromExtendedJSON: [Function (anonymous)],
index: 8101215
},
ReadPreference: [Function: readPref],
getConnection: [Function (anonymous)]
},
options: {
pluralization: true,
autoIndex: true,
autoCreate: true,
[Symbol(mongoose:default)]: true
},
_pluralize: [Function: pluralize],
Schema: [Function: Schema] {
reserved: [Object: null prototype] {
validate: 1,
toObject: 1,
save: 1,
remove: 1,
populated: 1,
isNew: 1,
isModified: 1,
init: 1,
get: 1,
errors: 1,
collection: 1,
removeListener: 1,
listeners: 1,
emit: 1,
prototype: 1
},
Types: {
String: [Function],
Number: [Function],
Boolean: [Function],
DocumentArray: [Function],
Subdocument: [Function],
Array: [Function],
Buffer: [Function],
Date: [Function],
ObjectId: [Function],
Mixed: [Function],
Decimal: [Function],
Decimal128: [Function],
Map: [Function],
Oid: [Function],
Object: [Function],
Bool: [Function],
ObjectID: [Function]
},
ObjectId: [Function: ObjectId] {
schemaName: 'ObjectId',
defaultOptions: {},
get: [Function (anonymous)],
set: [Function: set],
_checkRequired: [Function (anonymous)],
_cast: [Function: castObjectId],
cast: [Function: cast],
_defaultCaster: [Function (anonymous)],
checkRequired: [Function (anonymous)]
}
},
model: [Function (anonymous)],
plugins: [
[ [Function (anonymous)], [Object] ],
[ [Function (anonymous)], [Object] ],
[ [Function], [Object] ],
[ [Function (anonymous)], [Object] ],
[ [Function: trackTransaction], [Object] ]
],
default: [Circular *1],
mongoose: [Circular *1]
Reference: https://mongoosejs.com/docs/api/model.html#model_Model-base