Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 953
Naveen Avatar asked May 18 '26 20:05

Naveen


1 Answers

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

like image 174
michelem Avatar answered May 20 '26 11:05

michelem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!