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" : "[email protected]", "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" : "[email protected]", "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" : "[email protected]", "system.item": 1}   ).pretty()
{
    "_id" : ObjectId("56dd88578ff7fbd714091a4a"),
    "lastName" : "test",
    "firstName" : "test",
    "email" : "[email protected]",
    "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.

1 Answer 1

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": "[email protected]", 
        "system.item": 1
    }, 
    {
        "$addToSet": {
            "system.reading": {
                "adc1" : "123", 
                "adc2": "1245", 
                "id":"1" 
            }
        } 
    }
)
Sign up to request clarification or add additional context in comments.

3 Comments

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
id is a string, you can't increase it using the $inc update operator since it only acts on numerical values.
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"

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.