简体   繁体   中英

Inserting JSON object without JSON schema in Mongo DB through Mongoose (Node JS)

I have created a NodeJS project to log the JSON object in MongoDB which ever comes to my JAVA API. To insert the object, I have defined JSON schema and set it to the Mongoose object as show below.

var stateModel = require('./model/TeslaProfileRequest_JSONSchema.json');
var stateSchema = new mongoose.Schema(stateModel);
var State = mongoose.model('TeslaRequest', stateSchema);

This way, I am able to save my JSON object, but it is tightly coupled process. Because if TeslaProfileRequest_JSONSchema.json is changed in my Java API, I have to change the JSON schema in Node JS project also.

What I want to do is, without defining the JSON Schema in my Node JS project, I want to log the object what ever this API receives. Please help me.

In the schema just use a field as object and put the JSON there.

For example your schema could be something like this:

var mongoose = require('mongoose'),
    Schema = mongoose.Schema;

var JSONSchema = new Schema({
    created_at: Date,
    updated_at: Date,
    json: Object
});

mongoose.model('JSON', JSONSchema);

And then insert your JSON in the json field

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM