Categories
MongoDB

Using MongoDB with Mongoose — Maps and Getters

Spread the love

To make MongoDB database manipulation easy, we can use the Mongoose NPM package to make working with MongoDB databases easier.

In this article, we’ll look at how to use Mongoose to manipulate our MongoDB database.

Array Mixed Type

If we specify an empty array as the type, then the type is considered a mixed type.

For instance, if we have:

const mongoose = require('mongoose');
const connection = "mongodb://localhost:27017/test";
mongoose.connect(connection, { useNewUrlParser: true });
const db = mongoose.connection;
db.on('error', () => console.error('connection error:'));
db.once('open', () => {
  console.log('connected')
});
const Empty1 = new mongoose.Schema({ any: [] });
const Empty2 = new mongoose.Schema({ any: Array });
const Empty3 = new mongoose.Schema({ any: [mongoose.Schema.Types.Mixed] });
const Empty4 = new mongoose.Schema({ any: [{}] });

then they all have a any field with the mixed type.

Maps

Mongoose version 5.1.0 or later has the Map type to store key-value pairs.

For example, we can write:

const mongoose = require('mongoose');
const connection = "mongodb://localhost:27017/test";
mongoose.connect(connection, { useNewUrlParser: true });
const db = mongoose.connection;
db.on('error', () => console.error('connection error:'));
db.once('open', () => {
  console.log('connected')
});
const userSchema = new mongoose.Schema({
  socialMediaHandles: {
    type: Map,
    of: String
  }
});

const User = mongoose.model('User', userSchema);
console.log(new User({
  socialMediaHandles: {
    github: 'abc',
    twitter: '@abc'
  }
}).socialMediaHandles);

We created the userSchema with the Map type.

The of property sets the type of the value.

Then we can pass in the object to specify the key-value pairs for the map.

We can also write:

const mongoose = require('mongoose');
const connection = "mongodb://localhost:27017/test";
mongoose.connect(connection, { useNewUrlParser: true });
const db = mongoose.connection;
db.on('error', () => console.error('connection error:'));
db.once('open', () => {
  console.log('connected')
});
const userSchema = new mongoose.Schema({
  socialMediaHandles: {
    type: Map,
    of: String
  }
});

const User = mongoose.model('User', userSchema);
const user = new User({
  socialMediaHandles: {}
});
user.socialMediaHandles.set('github', 'abc');
user.set('socialMediaHandles.twitter', '@abc');
console.log(user.socialMediaHandles.get('github'));
console.log(user.get('socialMediaHandles.twitter'));
user.save();

We can access the socialMediaHandles property or put the property name in the string in the first argument of the set method.

And we can get the value by the key with the get method.

Getters

We can add getters to our schema fields.

For example, we can write:

const mongoose = require('mongoose');
const connection = "mongodb://localhost:27017/test";
mongoose.connect(connection, { useNewUrlParser: true });
const db = mongoose.connection;
db.on('error', () => console.error('connection error:'));
db.once('open', () => {
  console.log('connected')
});
const root = 'https://s3.amazonaws.com/mybucket';

const userSchema = new mongoose.Schema({
  name: String,
  picture: {
    type: String,
    get: v => `${root}${v}`
  }
});

const User = mongoose.model('User', userSchema);

const doc = new User({ name: 'Val', picture: '/123.png' });
console.log(doc.picture);
console.log(doc.toObject({ getters: false }).picture);

to add a getter to the picture field with the get method.

By default the picture property has the return value of the getter.

But we can also set the getters property to false to get the picture field’s value.

Conclusion

We can add array mixed types, getters, and maps to a schema field.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *