node.js - Inserting objects in MongoDB to field that can reference two different objects -
in project have messages model looks this:
var messageschema = new schema({ fromid: { type: entityid, required: true }, toid: { type: entityid, required: true }, important: { type: boolean, required: false }, lessonid: { type: schema.types.objectid, ref: models.lesson, required: false }, memberid: { type: schema.types.objectid, ref: models.lesson, required: false }, message: { type: string, required: true }});
as long message can sent , received either user , school i've created complex type 'entityid' looks this:
var entityid = { id: { type: schema.types.objectid, required: true }, entityname: { type: string, required: true, enum: entitytypelist.array }};
and i'm putting 'type' in fields fromid , toid in messageschema (as shown above). now, when try insert entities one:
samplemessage1: { _id:"560e9504cac9a42f0e415bca", memberid: "5631f6883ad9bc561889f006", deleted:true, fromid: { entityname:"school", id:"55d1e957daea17d3e90a3c51" }, toid: { entityname:"user", id:"569b89bb90d3ccd06d96644b" }, important:false, message:"sample message.", messagetype:"notification", read:true, timestamp:"2015-07-31t16:11:32.714z" }
using 'schema.create', in 'id' field, in database both in fromid , toid, instead of objectid string. know can this:
id: new objectid(55d1e957daea17d3e90a3c51")
but wonder if there's solution don't have change sample messages , can showed without using 'ref:'?
how inserting entities? if doing message.insert(data)
overriding mongoose. try create message first
var message = new message(data); message.save();
Comments
Post a Comment