我不明白为什么MongoDB中做到这一点,所以也许有人可以解释......下面是我的架构和代码,保存嵌套数组中的MongoDB
- 使用mongoose-
var postSchema = new mongoose.Schema({
raidName: String,
raidDate: String,
raidTime: String,
raidFaction: String,
whosGoing: { type: Array, "default": []}
});
var realmSchema = new mongoose.Schema({
realmName: String,
posts: [postSchema]
});
下面是
我的数据库看起来如何
{
"_id": {
"$oid": "581447f5fb4fab06dcbf10a6"
},
"realmName": "Cho'gall",
"posts": [
{
"raidName": "wailing cavs",
"raidDate": "Sat Oct 29 2016",
"raidFaction": "Horde",
"raidTime": "1:01 AM",
"_id": {
"$oid": "58144806fb4fab06dcbf10a7"
},
"whosGoing": [
{
"role": "DPS",
"gender": "Male",
"race": "Orc",
"classImg": "../images/class_photos/monk.png",
"class": "Monk",
"level": 90,
"name": "Monnky"
}
]
}
],
"__v": 1
}
我能够创造新的职位没有问题,但是 我不知道如何将更多的对象添加到“whosGoing”阵列.... 以下是林做..
var Realms = mongoose.model('realms');
Realms.findOne({realmName: realm}, function (err, realm) {
realm.posts.id(postId).whosGoing.push(thisChar); //pushing it here no problem
console.log(realm.posts.id(postId).whosGoing); //when i log it here both objects are in the array just fine
console.log(realm); //but when i log the realm here, the item i just pushed is not in there, so of course if I realm.save() it wouldnt take effect...
realm.save();
})
有人可以解释为什么发生这种情况,请..
这个作品很有意义,非常感谢! – Yandri