在字符串字段

时间:2016-04-01 17:31:32

标签: javascript json node.js mongodb

我正在使用Mongodb(mongoose)和node.js(express.js)。 DB的模型是:

var messageSchema   = new Schema({
    _channel: { type: Schema.ObjectId, ref: 'Channel', required: true },
    _user : { type: Schema.ObjectId, ref: 'User', required: true },
    datetime: { type: Date, required: true },
    messageType: { type: String, required: true },
    publish: { type: Boolean, default: false },
    content: {
        title: String,
        text: String,
    }
});

我想在文本字段(字符串)中保存JSON对象。 JSON对象是这样的:

{ event: 'push',
  repository: { id: 53012902, name: 'RestAPI' },
  ref: 'refs/heads/master',
  commits: 
   [ { id: 'a10202e5b5157ae5ccd2d77d7d578046693ae404',
       url: 'https://github.com/1izpena/RestAPI/commit/a10202e5b5157ae5ccd2d77d7d578046693ae404',
       author: '1izpena' } ],
  sender: { login: '1izpena', html_url: 'https://github.com/1izpena' } }

我在String中转换它,但结果是:

{"event":"push","repository":{"id":53012902,"name":"RestAPI"},"ref":"refs/heads/master","commits":[{"id":"a10202e5b5157ae5ccd2d77d7d578046693ae404","url":"https://github.com/1izpena/RestAPI/commit/a10202e5b5157ae5ccd2d77d7d578046693ae404","author":"1izpena"}],"sender":{"login":"1izpena","html_url":"https://github.com/1izpena"}}

这个结果不是String,我需要保持相同的格式以便稍后将其解析为json对象。 任何的想法? 非常感谢

0 个答案:

没有答案