1

thank you in advance for any help.

My problem is essentially to add data to a specific sub document.

I have the following models in my NodeJS server:

MODELS

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const dataSchema = new Schema({
    time: Date,
    value: String
});

const nodeSchema = new Schema({
    name: String,
    description: String,
    number: Number,
    status: String,
    lastSeen: Date,
    data: [dataSchema]
});

const siteSchema = new Schema({
    code: String,
    name: String,
    description: String,
    totalNodes: Number,
    nodes: [nodeSchema]
});

const Site = mongoose.model('site',siteSchema);
module.exports = Site;

Which basically looks like this:

Example

 {
        "_id": "5fa169473a394829bc485069",
        "code": "xfx3090",
        "name": "Name of this site",
        "description": "Some description",
        "totalNodes": 2,
        "__v": 0,
        "nodes": [
            {
                "_id": "5fa1af361e085b516066d7e2",
                "name": "device name",
                "description": "device description",
                "number": 1,
                "status": "Offline",
                "lastSeen": "2020-11-03T19:27:50.062Z",
                "data": [
                    {
                        "Date": "2019-01-01T00:00:00.000Z",
                        "value": "12"
                    },
                    {
                        "Date": "2019-01-01T00:00:00.000Z",
                        "Value": "146"
                    }
                ]
            },
            {
                "_id": "5fa1b10f4f24051520f85a58",
                "name": "device name",
                "description": "device description",
                "number": 2,
                "status": "Offline",
                "lastSeen": "2020-11-03T19:35:43.409Z",
                "data": [
                    {
                        "Date": "2019-01-01T00:00:00.000Z",
                        "Value": "555"
                    }
                ]
            }
        ]
    }
]

As you can see I have created two dummy nodes with some random data.

My question now is, say I want to add some data to Node 1. How will this code look?

I've tried many variations and attempted many different things without any luck. I know this would be easier by using the Object Id's, but I was hoping there is a way around this.

My Best result so far was with this code, but unfortunately it doesn't add any data.

addNodeData: async (req,res,next) => {
        const {siteCode} = xfx3090; //req.params
        const { nodeNumber } = 1;  //req. params - just to show example

        const nodeData = await Site.findOneAndUpdate({'code': siteCode, 'node.number': nodeNumber}, {$push: {'data':{'time': Date.now(), 'value':1223}}});

        res.status(200).json({message:'success'});
    }

Thank you in advance!

1 Answer 1

1

You need the positional operator $.

The query you want is something like this:

db.collection.update({
  "_id": "5fa169473a394829bc485069",
  "nodes._id": "5fa1af361e085b516066d7e2"
},
{
  "$push": {
    "nodes.$.data": {
      "Date": "newDate",
      "value": "newValue"
    }
  }
})

The first part is to find the document. I'm assuming nodes._id is not unique so I match _id too.

Then, with the pointer in the document you want to add the new data, you use $push into nodes.$.data. So, in the filed data there will be a new object.

A mongo plauground example is here

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

1 Comment

nice solution. I'd add the prototype update(<filter>, <update>, <options>). Looks like the site's name could be a good filter too.

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.