0

I have so complicated schema which had an inner array each array element contains one more level array. But how to update data inside arrays of the second level.

I need to update services array but how to get access? I have _id of Item, thread number, shift number (Can be only 3 and this not array due to three level arrays it's so hard), and service name.

I need something like (pseudocode)

getByItemId(id) .getByThreadNumber(tNumber) .getByShift(numberShift) .getByServiceName(name) .updateLogInCounter(newValCounter)

const itemSchema = new Schema({
    threads: [
        {
            number: {
                type: Number,
                requred: true
            },
            {
                firstShift: {
                    "someData": ... ,
                    "services": [
                        "name": {type: String}
                        "lastUpdate": {type: Date},
                        "logInCounter": {type: Number}
                    ]
                },
                secondShift: {
                    "someData": ... ,
                    "services": [
                        "name": {type: String},
                        "lastUpdate": {type: Date},
                        "logInCounter": {type: Number}
                    ]
                },
                thirdShift: {
                    "someData": ... ,
                    "services": [
                        "name": {type: String},
                        "lastUpdate": {type: Date},
                        "logInCounter": {type: Number}
                    ]
                }
            }
        }
    ]
})

Maybe this data structure incorrect and this impossible? Maybe I need to create many collections? Maybe some plugin helps me. Or it's normal, if yes how to get my target data?

1 Answer 1

1

You can use below code in 3.6

Use computed property names to dynamically pass the shift number. $[identifier] positional operator to match thread number and service name followed by increment logInCounter inside service for thread.

var query = {'_id': id};
var inc = {'$inc':{'threads.$[t].' + [numberShift] + '.services.$[s].logInCounter':newValCounter}};
var arrayFilters = {arrayFilters: [{'t.number': tNumber, 's.name':name}]};
Item.findOneAndUpdate(query, inc, arrayFilters);
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.