Skip to content

Latest commit

 

History

History
49 lines (36 loc) · 1021 Bytes

README.md

File metadata and controls

49 lines (36 loc) · 1021 Bytes

Mongoose Dictionary Plugin

How to use

npm install --save mongoose HabitRPG/mongoose-dictionary
var mongoose = require('mongoose');
var dictionaryPlugin = require('mongoose-dictionary')(mongoose);

mongoose.connect('mongodb://localhost/dictionary_testing');

var MySchema = new mongoose.Schema({
  title: String,
});

MySchema.plugin(dictionaryPlugin, {
  fields: {
    nameOfTheDictionaryField: {
      aFieldOfDictionary: String
    }
  }
});

var MyModel = mongoose.model('MyModel', MySchema);

var doc = new MyModel({
  title: 'a'
});

// IMPORTANT THIS MUST BE CALLED WHEN CREATING A NEW DOCUMENT LOCALLY (NOT RETURNED FROM DB)
doc.emit('new', doc);

doc.dictionary.$add('key', {
  aFieldOfDictionary: 123 // Using a number to show that casting is working
});

doc.dictionary.$add('anotherKey', {
  aFieldOfDictionary: 123 // Using a number to show that casting is working
});

doc.dictionary.$remove('anotherKey');

doc.save(function(err, doc){
  console.log(err, doc)
});