user4367388
user4367388

Reputation:

Complex Schema with subdocument

UPDATE:

Currently i have this schema:

var Schema = new schema ({
    name: String,
    type: { type: String },
    date: { type: Date },
    descrip: String,
});

But i use this schema for generate 2 documents:

THE A TYPE ({
    name: 'A',
    type: 'TYPE_B',
    date: { type: Date },
    descrip: 'DESC_A',
});

THE B TYPE ({
    name: 'B',
    type: 'TYPE_B',
    date: { type: Date },
    descrip: 'DESC_B',
});

The name, type and descrip are always the same in the A and B types, the only thing that changes is the date field, so i was thinking, how can i improve this? How can i insert several dates in the same schema, instead of create always an document with the same name, type and descrip values?

So i am trying to create a schema inside other schema, but i dont know if this is possible, it is?

I was trying like this:

var mainSchema = new schema ({
    name: String,
    type: { type: String },
    date: [ dateSchema ],
    descrip: String,
});

var dateSchema = new Schema ({
    date: {
     type: Date
    }  
});

What i want, is create two mainSchema, type a and type b, and insert dates inside...

Am i doing this right? How can i achieve my goals?

I am searching for a full answer with good explanation, that's why the bounty. I don't accept +/- answers.

Upvotes: 2

Views: 230

Answers (2)

SUNDARRAJAN K
SUNDARRAJAN K

Reputation: 2295

To create a record with multiple dates you can use array of Dates.

var mainSchema = new schema ({
name: String,
type: { type: String },
dates: [Date],
descrip: String,
});

The documents will be as follows

THE A TYPE ({
name: 'A',
type: 'TYPE_B',
dates: ["2014-01-22T14:56:59.301Z","2015-01-22T14:56:59.301Z"],
descrip: 'DESC_A',
});

THE B TYPE ({
name: 'B',
type: 'TYPE_B',
dates: ["2015-01-22T14:56:59.301Z","2014-01-22T14:56:59.301Z"],
descrip: 'DESC_B',
});

Reference: http://mongoosejs.com/docs/schematypes.html

For saving the document you can use like.

exports.save = function(req,res){
var test = new newSchema;  // new object for newSchema domain.
test.name= req.body.name;
test.type= req.body.type;
test.desc= req.body.desc;
if(req.body.date){
    req.body.forEach(function(date){  // For every element of date from client.
        test.date.push(date)  // This pushes each and every date given from the client into the date array.
    })
}
test.save(function (saveErr, saved) {  // Saves the new document into db.
    if (saveErr) {
        console.log(saveErr)
        return;
    }
    res.status(HttpStatus.OK).json(saved);
});
};

For Update you can use like.

exports.update = function(req,res){  
newSchema.findOne({name: 'B'},function(err,test){  // Finds a record with name:'B' and stores in test the same can be done for name:'A'.
    if(test){
        if(req.body.date){
            req.body.forEach(function(date){
                test.date.push(date)  // pushes dates into existing test document (update).
            })
        }
        test.save(function (saveErr, saved) {  // Saves (updates) the document.
            if (saveErr) {
                console.log(saveErr)
                return;
            }
            res.status(HttpStatus.OK).json(saved);
        });
    }
});
};

Hope this helps.

Upvotes: 6

winston86
winston86

Reputation: 159

If your code write down in one section it will look like this

var mainSchema = new schema ({
    name: String,
    type: { type: String },
    date: [ {date: { type: Date } } ],
    descrip: String,
});

I think that's wrong. Also you don't need to creat new schema like this, you can use just {} in your code http://www.w3schools.com/js/js_objects.asp . I think correct code must look like this:

var mainSchema = {
 name: string,
 date: {},
 descrip: string
}

And then push into 'date' your dates. Good luck.

======================== HOW TO USE =======================

Sorry for delay. You can use mainSchema.date.date_1 = date; or you can chang date: {}, to date: [], and use mainSchema.date.push(date) depend on your needs.

Upvotes: 1

Related Questions