1

I have a schema :

{
    "id": String,
    "username": String,
    "password": String,
    "email": String,
    "firstName": String,
    "lastName": String,

    "system" : {
            "item" : {type: Number},
            "update_interval" :  { type: Number, max: 99999 },
            "reading" : [
                {
                    "id" :              { type: Number},
                    "adc1"  :           { type: Number, max: 4095 },
                    "adc2"  :           { type: Number, max: 4095 },
                    "pc_datestamp" :Date,
                }
            ]
    }

now i want to add values to

"reading" : [
                    {
                        "id" :              { type: Number},
                        "adc1"  :           { type: Number, max: 4095 },
                        "adc2"  :           { type: Number, max: 4095 },
                        "pc_datestamp" :Date,
                    }
                ]

but i dont know where I am wrong I have tried to update data from mongoshell but no success till now

> db.users.update( {"email" : "test@test.com", "system.item": 1,   }, {"$push": {"system.$.reading": [{"adc1" : "123", "adc2": "1245", "id":"1" }] } })
WriteResult({
    "nMatched" : 0,
    "nUpserted" : 0,
    "nModified" : 0,
    "writeError" : {
        "code" : 16837,
        "errmsg" : "The positional operator did not find the match needed from the query. Unexpanded update: system.$.reading"
    }

> db.users.update( {"email" : "test@test.com", "system": {$elemMatch:{ item: 1}}   }, {"$push": {"system.$.reading": {"adc1" : "123", "adc2": "1245", "id":"1" } } })
WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })

I have set the value of item as one

> db.users.find( {"email" : "test@test.com", "system.item": 1}   ).pretty()
{
    "_id" : ObjectId("56dd88578ff7fbd714091a4a"),
    "lastName" : "test",
    "firstName" : "test",
    "email" : "test@test.com",
    "password" : "$2a$10$wY9wr9oreza4fBX7CfXym.rPZUPrcesigYIfWd0zbM4dDjBy6k3vy",
    "username" : "test",
    "system" : {
        "item" : 1,
        "reading" : [ ]
    },
    "__v" : 0
}

I have followed

Mongodb $push in nested array

and this Insert data in nested array in mongodb

and many more questions but cannot find whats wrong.

Community
  • 1
  • 1
gurumonk
  • 38
  • 1
  • 1
  • 5

1 Answers1

0

Since the positional $ operator acts as a placeholder for the first element that matches the query document, and the array field must appear as part of the query document, your update operation does not satisfy these conditions hence it suffers from the error fate you are getting. In your query you are only referencing the "system.item" field which is not an array.

The other option you can do is ditch the positional $ operator in your update and just add the object to the array using $addToset which adds elements to an array only if they do not already exist in the set:

db.users.update(
    {
        "email": "test@test.com", 
        "system.item": 1
    }, 
    {
        "$addToSet": {
            "system.reading": {
                "adc1" : "123", 
                "adc2": "1245", 
                "id":"1" 
            }
        } 
    }
)
chridam
  • 100,957
  • 23
  • 236
  • 235
  • Thanks @chridam, That was very helpful.. I have one more doubt how do I increase the value of "id" as i keep adding the values – gurumonk Mar 07 '16 at 15:28
  • `id` is a string, you can't increase it using the `$inc` update operator since it only acts on numerical values. – chridam Mar 07 '16 at 15:33
  • i dont understand "id" : { type: Number}, here i have defined it as number , or is it something like type: [number], Is there any way to increase the "id" – gurumonk Mar 07 '16 at 15:52