0

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:

Model:

var NetworkSchema = new Schema({
UserID: {
    type: String,
    default: '',
    trim: true
},
NetworkList: [{
    type: Schema.ObjectId,
    ref: 'User'
}]
});

Server Controller:

exports.update = function(req, res) {
    var query={'UserID': req.body.UserID};
    var update = {$push: {'NetworkList': req.body.FriendID}};
};

Now my model is :

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'
}]
}]

});

and i want to append inside both array's NetworkRequest and NetworkList, so i tried to use $push with both like

Server Controller:

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');
}
});

Document:

{
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")
}

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?

1 Answer 1

1

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');
}
});
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.