Javascript 从mongoose模型传递嵌套JSON数据无效

Javascript 从mongoose模型传递嵌套JSON数据无效,javascript,json,node.js,mongoose,mongoose-schema,Javascript,Json,Node.js,Mongoose,Mongoose Schema,我正在将嵌套的JSON数据推送到数据库中。这就是我的模式的样子 const mongoose = require('mongoose'); // original Schema const dataSourceSchema = mongoose.Schema({ _id: mongoose.Schema.Types.ObjectId, name: { type: String, required: true, unique: true }, type: { type:

我正在将嵌套的JSON数据推送到数据库中。这就是我的模式的样子

const mongoose = require('mongoose');
// original Schema

const dataSourceSchema = mongoose.Schema({

    _id: mongoose.Schema.Types.ObjectId,
    name: { type: String, required: true, unique: true },
    type: { type: String, required: true },
    projectId: { type: mongoose.Schema.Types.ObjectId, ref: 'Project', required: true },
    config:{type: String, required: true}

});

module.exports = mongoose.model('DataSource', dataSourceSchema);
我想将以下json数据传递到我的dataSourceSchema

{
“name”:”JdbcSourceConnector”,
"type" :"string",
“config”: {
“connector.class”:” io.confluent.connect.jdbc.JdbcSourceConnector”,
“tasks.max”:1,
“connection.url”:”<connection to connect to database along with username and password>”,
“mode”:”incrementing”,
“incrementing.column.name”:”<incrementing column name in table>”,
“topic.prefix”:”test-mysql-jdbc-”
}
}
此模式也会给我错误,预期为“,”

如果像我在原始模式中提到的那样只传递一个字符串,那么数据将存储在db中。 但是我想传递嵌套的json数据,请引导我正确的方向。
我还尝试了将数据字符串化,但它不起作用。

在我看来,错误在于用第二个模式定义您的模式,而您已经接近答案了。按如下方式更改您的架构:

const dataSourceSchema = mongoose.Schema({
      _id: mongoose.Schema.Types.ObjectId,
      name: { type: String, required: true, unique: true },
      type: { type: String, required: true },
      projectId: { type: mongoose.Schema.Types.ObjectId, ref: 'Project', required: true },
      config:{ 
            connectorClass:{ type: String, required: true },
            tasksMax:{ type: String, required: true },
            connectionUrl:{ type: String, required: true },
            mode:{ type: String, required: true },
            incrementingColumnName:{ type: String, required: true },
            topicPrefix:{ type: String, required: true }
      }
});
我建议进行更改,因为mongoose不理解其模式的键列中的点符号,因此您收到了您提到的错误

如果出于某种原因想使用点表示法,请将键封装在“中”,而不是代码段中出现的特殊字符中


注意-不要忘记更改json中的键名

您正在使用的
是一个特殊字符,请使用
以避免强制转换错误,对于类似
配置:{type:Schema.Types.Mixed,required:true}的配置,请使用
Schema.Types.Mixed
而不是
String
谢谢Arifkhan,我会试试这个。谢谢aashisAilawadi,我想在我的json数据中用点(.)传递。你能帮我用数据中的(.)解析数据吗。@darshanan,我看到你用的是“而不是常规的”字符“。我建议您在编辑器中再次键入双引号,并检查它们是否为标准双引号。
const dataSourceSchema = mongoose.Schema({
      _id: mongoose.Schema.Types.ObjectId,
      name: { type: String, required: true, unique: true },
      type: { type: String, required: true },
      projectId: { type: mongoose.Schema.Types.ObjectId, ref: 'Project', required: true },
      config:{ 
            connectorClass:{ type: String, required: true },
            tasksMax:{ type: String, required: true },
            connectionUrl:{ type: String, required: true },
            mode:{ type: String, required: true },
            incrementingColumnName:{ type: String, required: true },
            topicPrefix:{ type: String, required: true }
      }
});