猫鼬使用多个阵列内追加 [英] Append within Multiple Arrays using Mongoose
问题描述
我试图处理的朋友或用户它由逗号列表分隔对象ID的用户不同,现在我已经 NetworkList 阵列存储,用户列表,到目前为止,我用$推追加对象ID的在列表中的 NetworkList 阵列的位置:
i am trying to handle list of friends or users which consists of comma separated ObjectID's of different users, now i have NetworkList array which stores, list of users, so far i used $push to append list of ObjectID's in NetworkList array here:
型号:
var NetworkSchema = new Schema({
UserID: {
type: String,
default: '',
trim: true
},
NetworkList: [{
type: Schema.ObjectId,
ref: 'User'
}]
});
服务器控制器:
exports.update = function(req, res) {
var query={'UserID': req.body.UserID};
var update = {$push: {'NetworkList': req.body.FriendID}};
};
现在我的模式是:
var NetworkSchema = new Schema({
UserID: {
type: Schema.Types.ObjectId,
ref: 'User'
},
NetworkList: [{
type: Schema.Types.ObjectId,
ref: 'User'
}],
NetworkRequest: [{
to:[{
type: Schema.Types.ObjectId,
ref: 'User'}],
from:[{
type: Schema.Types.ObjectId,
ref: 'User'
}]
}]
});
,我想这两个阵列的NetworkRequest和NetworkList内追加,所以我尝试使用$推既像
and i want to append inside both array's NetworkRequest and NetworkList, so i tried to use $push with both like
服务器控制器:
exports.update = function(req, res) {
var query={'UserID':req.body.UserID};
var update = {$push: {'NetworkRequest':{to: req.body.FriendID, from: req.body.McReg}, 'NetworkList': req.body.FriendID}};
Network.find(query,function(err,user){
console.log(user);
if (err) {
return err;
} else {
console.log('no error');
}
});
Network.update(update,function(err){
if (err) {
return err;
} else {
console.log('Updated');
}
});
文件:
{
NetworkList: [
ObjectId("5490098c9f2652f01b3f68df"),
ObjectId("5490195f5afa90e01b500c37"),
],
NetworkRequest: {
_id: ObjectId("54902e9375fff9b01a007516"),
from: [ObjectId("5490195f5afa90e01b500c37")],
to: [
ObjectId("54883e606d574c000feccb08"),(//not able to append here)
]
},
UserID: ObjectId("5490195f5afa90e01b500c37"),
__v: 0,
_id: ObjectId("5490195f5afa90e01b500c38")
}
我使用的console.log(更新);我在哪里能得到OBJECTID的内线两个数组的,但最后的值不起床到数据库中,在那里我与$推回事?
i used console.log(update); where i am able to get objectID's inside both array's, but finally values are not getting up to the database, where i am going wrong with $push?
推荐答案
要推子阵里面,你应该使用点操作符为您NetworkRequest阵列。
To push inside sub Array, you should use dot operator for your NetworkRequest Array.
exports.update = function(req, res) {
var query={'UserID':req.body.UserID};
var update = {$push: {'NetworkRequest.to': req.body.FriendID, 'NetworkRequest.from': req.body.McReg}, 'NetworkList': req.body.FriendID}};
Network.update(query,update,{upsert:true}function(err){
if (err) {
return err;
} else {
console.log('Updated');
}
});
}
这篇关于猫鼬使用多个阵列内追加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!