0

I have tried to add the element inside JSON. When I take the data into variable and try to add, I am able to. But, When I try to do the same operation inside the MongoDB find function, Its not working. This question is continuation to this

app.get ("/structures", function(req, res){
    if(req.query.type=="things"){
    mongoOp.thingsSt.find({},'-_id uuid devices', function(err, data) {
      var entityID=[];
      for (var i =0; i<data.length; i++){
         data[i].entityID = "1234";
         console.log(data);
       }
            res.send(data);
});

Data:

[ { devices: { dID: 'TLSM01', deviceList: [Object] },
    uuid: 'e863c776-f939-4761-bbce-bf0501b42ef7' },
  { devices: { dID: 'TLSM01', deviceList: [Object] },
    uuid: '5a0cd70d-891d-48d8-b205-e92e828ac445' } ]

Expected Output:

[ { devices: { dID: 'TLSM01', deviceList: [Object] },                                                                                                                                                                      
    uuid: 'e863c776-f939-4761-bbce-bf0501b42ef7',                                                                                                                                                                          
    entityID: '1234' },                                                                                                                                                                                                    
  { devices: { dID: 'TLSM01', deviceList: [Object] },                                                                                                                                                                      
    uuid: '5a0cd70d-891d-48d8-b205-e92e828ac445',                                                                                                                                                                          
    entityID: '1234' } ] 

When I try to do it with variable holding my data, I can able to achieve

var data = [ { devices: { dID: 'TLSM01', deviceList: ["Object"] },
    uuid: 'e863c776-f939-4761-bbce-bf0501b42ef7' },
  { devices: { dID: 'TLSM01', deviceList: ["Object"] },
    uuid: '5a0cd70d-891d-48d8-b205-e92e828ac445' } ]

for (var i =0;i<data.length;i++){
    data[i].entityID = "1234"
}
console.log(data);
Community
  • 1
  • 1

2 Answers2

0

to add new field to mongodb existing collection you need to update the collection check this SO Add new field to a collection in MongoDB

> db.foo.find()
> db.foo.insert({"test":"a"})
> db.foo.find()
{ "_id" : ObjectId("4e93037bbf6f1dd3a0a9541a"), "test" : "a" }
> item = db.foo.findOne()
{ "_id" : ObjectId("4e93037bbf6f1dd3a0a9541a"), "test" : "a" }
> db.foo.update({"_id" :ObjectId("4e93037bbf6f1dd3a0a9541a") },{$set : {"new_field":1}})
> db.foo.find()
{ "_id" : ObjectId("4e93037bbf6f1dd3a0a9541a"), "new_field" : 1, "test" : "a" }
Community
  • 1
  • 1
Nair Athul
  • 823
  • 9
  • 21
  • I don't need to add field into mongoDB Collection. I just need to add a new field dynamically inside the function after the document is retrieved. – Karthikeyan Prakash May 03 '17 at 05:02
  • I think then you just assign that value to another variable and try change it there. That will be fine. because if you dont need to change values in mongo why you need to change the data object – Nair Athul May 03 '17 at 05:08
0

Your data is not a plain javascript object. It is a MongoDB Document and thus read-only in a find result. I'm not sure if MongoDB by itself has a .lean method such as the one supplied by MongooseJS. Either way, you're going to need to convert the MongoDB document into a plain javascript object first.

Kyle Richardson
  • 5,567
  • 3
  • 17
  • 40