所以我有一个这样的架构:每个机构都有X个专业人士。
var mongoose = require('mongoose');
var profSchema = new mongoose.Schema(
{
userRefID : { type: mongoose.Schema.ObjectId, ref: 'User' },
Status : Boolean,
initLic : Date,
endLic : Date
});
var instSchema = new mongoose.Schema(
{
name : String,
professional : [profSchema]
});
mongoose.model('Inst', instSchema);
var Inst = mongoose.model('Inst');
mongoose.model('Prof', profSchema);
var Prof = mongoose.model('Prof');
数据如下所示,假设我想将intitutionA的ObjectId(“56e05bbfb37d4c7c4c2c45b7”)专业人员转移到INSTITB并更新其InitLic和EndLic字段,我怎么能用mongoose实现呢?我一直试图通过找到第一个父元素中的元素然后将其推入另一个元素同时删除第一个父元素中的副本来解决这个问题,但这对我来说似乎不对。
{
"_id" : ObjectId("56e05b70b37d4c7c4c2c45b3"),
"name" : "InstituitionA",
"professional" : [
{
"endLic" : ISODate("2016-04-08T23:00:00.000Z"),
"initLic" : ISODate("2016-03-09T17:20:49.523Z"),
"Status" : true,
"userRefID" : ObjectId("56e05b7cb37d4c7c4c2c45b4"),
"_id" : ObjectId("56e05b7cb37d4c7c4c2c45b5")
},
{
"endLic" : ISODate("2016-04-08T23:00:00.000Z"),
"initLic" : ISODate("2016-03-09T17:21:57.441Z"),
"Status" : true,
"userRefID" : ObjectId("56e05bbeb37d4c7c4c2c45b6"),
"_id" : ObjectId("56e05bbfb37d4c7c4c2c45b7")
}
],
"__v" : 0
}
{
"_id" : ObjectId("56e05c5f35ae7b6017e39ad4"),
"name" : "InstituitionB",
"professional" : [],
"__v" : 0
}