0

Hello I would like to push a new object in critaire3, any ideal how to do that?

Schema

 _id:ObjectId,
Teacher:[{
    id:Number,
    nom:String,
    surnom:String,
    password:String,
    cle:String
}]
,
Etudiant:[{
    id:Number,
    nom:String,
    surnom:String,
    matricule:String,
    password:String    
}],

Chef_de_departement:{
    nom:String,
    password:String,
    cle:String
},

Uv:[{
    code:String,
    nom_eng:String,
    cle:String,
    title:String,
    votes:[{
        critaire1:[{
            vote:Number
        }],
         critaire2:[{
            vote:Number
        }],
        critaire3:[{
            vote:Number
        }],
        critaire4:[{
            vote:Number
        }], 
        critaire5:[{
            vote:Number
        }], 
        critaire6:[{
            vote:Number
        }],
         critaire7:[{
            vote:Number
        }],
         critaire8:[{
            vote:Number
        }],
         critaire9:[{
            vote:Number
        }],
         critaire10:[{
            vote:Number
        }],
        critaire11:[{
            vote:Number
        }]

     }]

}]

Output

"status": "success",
    "data": [{
        "_id": "5a2bb6913e97dbac1de25948",
        "__v": 1,
        "Uv": [{
            "code": "Info204",
            "nom_eng": "stanly",
            "cle": "kdkf",
            "title": "algo  and uml",
            "votes": [{
                "critaire11": [],
                "critaire10": [{}],
                "critaire9": [{}],
                "critaire8": [{}],
                "critaire7": [{}],
                "critaire6": [{}],
                "critaire5": [{}],
                "critaire4": [{}],
                "critaire3": [{}],
                "critaire2": [{}],
                "critaire1": [{}]
            }]
        }],

Tried

server.put("/add/votes", function(req, res, next) {

UserModel.update({
        _id: myid,
        'Uv.code': "Info204"
    }, {
        $push: {
            'plan.$.votes.$.critaire3': "df"
        }
    }, {
        upsert: true
    }, function(err, model) {
        if (err) {
            helper.failure(res, next, 'Something went wrong while fetching the user from the database', 500);
            return next();
        }

    helper.success(res, next, model.Uv);

});

});

Don't know why, but it does not go through!

Kiran LM
  • 1,359
  • 1
  • 16
  • 29
Stanly Medjo
  • 85
  • 1
  • 3
  • 12
  • Multiple positional operator are not support in pre 3.6 version. For 3.6 version you can try `UserModel.update( {_id: myid }, { $push: { "Uv.$[u].votes.$[v].critaire3": "df" } }, { upsert: true, arrayFilters: [ { 'u.code': "Info204" }, { "v.critaire3": {$exists:true} } ] } )` – s7vr Dec 12 '17 at 14:02
  • Possible duplicate of [Updating a Nested Array with MongoDB](https://stackoverflow.com/questions/23577123/updating-a-nested-array-with-mongodb) – s7vr Dec 12 '17 at 14:05
  • @veeram thanks for the fast reply please what is the use of [u] and the [v] in the code – Stanly Medjo Dec 12 '17 at 14:09
  • Np. It is a placeholder which finds the matching array index from array filters provided as the last argument to the query. So [u] will be replaced from the matching index found from `{ 'u.code': "Info204" }` and same for [v] variable. So your query will transform to `"Uv.0.votes.0.critaire3"` and pushes the array at that location. – s7vr Dec 12 '17 at 14:13
  • okay thanks, please am to push an object in critaire3 that is {vote:3}, how can i go about that, cause your solution only provides the option to push a value – Stanly Medjo Dec 12 '17 at 14:16
  • Np. Value or document it should not matter. Something like { `$push: { "Uv.$[u].votes.$[v].critaire3": {vote:3}}` – s7vr Dec 12 '17 at 14:17
  • @veeram please after trying that method i get an error while fetching the user – Stanly Medjo Dec 12 '17 at 16:09
  • Is update working for you ? If no what is the error you get, if yes then it seems like a separate issue. perhaps a different question ? – s7vr Dec 12 '17 at 16:54
  • @veeram TypeError : cannot use 'in' operator to search for '_id' in {votes:3} – Stanly Medjo Dec 12 '17 at 17:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/161048/discussion-between-veeram-and-stanly-medjo). – s7vr Dec 12 '17 at 17:27

0 Answers0