2

I'm pretty new to js and mongoose and I've ended up with a nested JSON.

var standardmessage = {
        "id": "352",
        "name": "a name",
        "type": "a type",
        "message":
            {
                "messagetype": "default message",
                "timestamp": "35235326326",
                "messagestatus": "running"
            }
    }

Now I tried to define a schema:

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


var messageSchema  = new Schema({
    id: Number,
    name: Schema.Types.Mixed,
    type: Schema.Types.Mixed,
    message:
            {
                messagetype: String,
                timestamp: Number,
                messagestatus: String
            }
}, {collection: 'test'});

var testmodel = mongoose.model('messagemodel', messageSchema,'test');

module.exports = testmodel;

Finally I tried to store it through Mongoose into MongoDB:

var Message = new testmodel({standardmessage});
                Message.save(function (err) {
                if (err) console.log(err);
                });

Either my schema is wrong and i need multiple schemas to store it correctly or sth. else is wrong. But in my database it only get's stored like this:

{ "_id" : ObjectID("xxxxxxxxxxx"), "__v" : 0}

Is it possible to define one Schema to store this nested JSON? If yes, how? If not, how do I manage to store it correctly?

Why does it only store an ObjectID?

Thanks in advance, appreciating every kind of help and sorry if my question is stupid as im new to this.

Update:

    var messageSchema  = new Schema({
      id: Number,
      name: Schema.Types.Mixed,
      type: Schema.Types.Mixed,
      message: [message]

     }, {collection: 'test'});


    var message = new Schema({
    "messagetype": "default message",
     "timestamp": "35235326326",
     "messagestatus": "running"
     })

Then it gets stored like this { "_id" : ObjectID("xxxxxxxxxxx"), "message" : [], "__v" : 0}

3
  • pls show your data in (standardmessage) field Commented Jun 2, 2016 at 10:03
  • 1
    Using the sample you provided, I got it working. I only had to remove the {} from 'var Message = new testmodel({standardmessage});' Commented Jun 2, 2016 at 10:04
  • That can't be real :( Almost a day for just removing those brackets.. Coding can be so sad.. Thanks a lot. Commented Jun 2, 2016 at 10:12

1 Answer 1

1

For a nested JSON, you can do something like this:

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

var standardmessage = new Schema({
  id: Number,
  name: String,
  type: String,
  message: {
    messageType: String,
    timestamp: Number,
    messagestatus: String
  }
});

If the nested block is a list then you can do this:

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

var msg = new Schema({
  messageType: String,
  timestamp: Number,
  messagestatus: String
});

var standardmessage = new Schema({
  id: Number,
  name: String,
  type: String,
  message: [msg]
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.