0

i am trying to push inside a subarray using $push but got a Mongo error, and not able to get through this after considerable search on google, and findOneAndUpdate didn't worked out so i used find and update separately

{ [MongoError: can't append to array using string field name: to]
name: 'MongoError',
err: 'can\'t append to array using string field name: to',
code: 13048,
n: 0,
lastOp: { _bsontype: 'Timestamp', low_: 2, high_: 1418993115 },

Schema:

var NetworkSchema = new Schema({

UserID: {
    type: Schema.Types.ObjectId,
    ref: 'User'
},
NetworkList: [{
    type: Schema.Types.ObjectId,
    ref: 'User'
}],

NetworkRequest: [{
    from: [{
    type:Schema.Types.ObjectId,
        ref: 'User'
}],
    to: [{
        type: Schema.Types.ObjectId,
        ref: 'User'
    }]
}]

});

Document:

{
"UserID" : ObjectId("549416c9cbe0e42c1adb42b5"),
"_id" : ObjectId("549416c9cbe0e42c1adb42b6"),
"NetworkRequest" : [ 
    {
        "from" : [],
        "to" : []
    }
],
"NetworkList" : [],
"__v" : 0
}

Controller:

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

Network.find(query,function(err){
    if (err) {
        console.log(err);
        return err;
    } else {

    }
});

Network.update(query,update,{upsert:true},function(err,user){
    console.log(user);
    if (err) {
        console.log(err);
        return err;
    } else {

        console.log('User'+user);
    }
});
};
4
  • Not sure if it's the only problem, but NetworkRequest is defined as an object in your schema but in your doc it's an array. Also, why is the Network.find call there? Commented Dec 19, 2014 at 14:30
  • Sorry my apologies, NetworkRequest is an Array in Schema and i edited the question now, mongoose findOneAndUpdate was not working as when i used this function, no errors were shown but i was not able to append values to the array in my database, so i used Network.find and Network.update separately, it was working nicely until this error of MongoDb shown as console, please help me out, i hope you understand my situation here and if not, then i will explain it to you in more detail Commented Dec 19, 2014 at 16:22
  • 1
    The Network.find call isn't needed. Just remove it. Commented Dec 19, 2014 at 18:00
  • ok! i did but still getting that mongo error!! Commented Dec 20, 2014 at 10:09

2 Answers 2

1

Everything @cbass said in his answer is correct, but since you don't have a unique identifier in your NetworkRequest element to target, you need to do it by position:

var query = {'UserID': req.body.UserID};
var update = {$push:{'NetworkRequest.0.to': req.body.FriendID}};
Test.update(query, update, {upsert: true}, function(err, result) { ... });

'NetworkRequest.0.to' identifies the to field of the first element of the NetworkRequest array.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for answer! Can you tell me how can i update two different documents having different ObjectID's inside same UPDATE function?
@shaileshshekhawat Better to post that as a separate question.
ok this Question i asked here: -stackoverflow.com/questions/27582112/…
1

Your query var query={'UserID':req.body.UserID}; identifies the document you want to edit. Then you need another query to identify which object in the NetworkRequest array that the UserID should be pushed into. Something like below:

var query = { 
       'UserID':req.body.UserID,
       'NetworkRequest._id': ObjectId(someNetworkRequestId)
};

Then use this update query containing $ which is the index of the object in the nested array(NetworkRequest)

var update = {
         $push:{
            'NetworkRequest.$.to': req.body.FriendID
         }
};

7 Comments

no it's not working when i used $ sign i am getting this error quite different from above error--- MongoError: Cannot apply the positional operator without a corresponding query field containing an array.---
Do you have the id for the networkrequest you want to push into?
yes i have id that's why i didn't mention client controller side code, as it's not causing any error
sorry!.i am not able to get this line of code--'NetworkRequest._id': ObjectId(someNetworkRequestId)-- can you help me, what should i refer to ObjectId(someNetworkRequestId), just for information here, UserID is id of logged in user.
Sorry, now I can see that the NetworkRequest doesn't have an Id. Which makes the push extremely hard. But I don't understad why NetworkRequest is an array. You don't have anything that indetifies it.
|

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.