MongoDBドキュメントのキー名に "." が入ってると怒られます。
JSONのつもりでいたけどそうじゃなかった
"use strict"; function Tags () { this.tags = {}; return this; } (function (tp) { tp.reg = /#([^#\s\(\)\[\]]+)/g; tp.index = function () { return Object.keys(this.tags); }; tp.map = function () { for (var i = 0, len = arguments.length, _str; i < len; i++) { _str = arguments[i]; if (typeof _str === 'string' && /#/.test(_str)) { var reg = this.reg; var str = _str.toLowerCase(); var res, matching; while (res = reg.exec(str)) { matching = res[1]; if (! this.tags[matching]) this.tags[matching] = 0; this.tags[matching]++; } } } return this; }; tp.get = function (tagname) { return this.tags[tagname]; }; })(Tags.prototype); var mongoose = require('mongoose'); var mongos = 'mongodb://localhost' + '/HogeHoge'; var HogeSchema = new mongoose.Schema({ body: String , tags: {} // このキー名で引っかかった }); mongoose.connect( mongos ); console.log('[mongoose] MongoDB connected "%s"', mongos); var Hoge = mongoose.model('Hoge', HogeSchema); function test (body, i) { var tagClient = new Tags; var hoge = new Hoge({body: body, tags: {}}); tagClient.map(body).index().forEach(function (tagname) { hoge.tags[tagname] = tagClient.get(tagname); }); hoge.save(function (err) { console.log('test -%d: "%s"', i, body); (err) ? console.error(err) : console.log(hoge); }); } [ 'foo' , 'foo.hoge' , '.hoge' , '#foo' , '#foo.hoge' , '#node.js' ].forEach(test);
結果
[mongoose] MongoDB connected "mongodb://localhost/HogeHoge" test -4: "#foo.hoge" [Error: key foo.hoge must not contain '.'] test -5: "#node.js" [Error: key node.js must not contain '.'] test -3: "#foo" { body: '#foo', tags: { foo: 1 }, _id: 504bf47b987c4d32ed000004 } test -2: ".hoge" { body: '.hoge', tags: {}, _id: 504bf47b987c4d32ed000003 } test -1: "foo.hoge" { body: 'foo.hoge', tags: {}, _id: 504bf47b987c4d32ed000002 } test -0: "foo" { body: 'foo', tags: {}, _id: 504bf47b987c4d32ed000001 }