Naveen
Naveen

Reputation: 947

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.

Upvotes: 3

Views: 3287

Answers (1)

michelem
michelem

Reputation: 14590

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

Upvotes: 4

Related Questions