dexie-mongoify icon indicating copy to clipboard operation
dexie-mongoify copied to clipboard

how to push to array in a nested object

Open akolnati opened this issue 7 years ago • 9 comments

I am trying to add an object to an existing array which in inside a nested object. The structure looks similar to below

{ 
  Name : 'John',
  age : 33,
  tags : {
     skill: [{
         first: '.NET',
         second: 'JAVA',
         third: [{special1:'sleep'},{special2:'eat'}] 
     }]
  }
}

I have tried many way to push object special3:'run' to skill.third but without success. My last attempt looked something like this

const pathObject = {};
const fullPath = 'result.tags.skill[3].third';
pathObject[fullPath] = {special3:'run'};
db.inspections.update(id, pathObject);

The object is added outside and not inside the array 'third' something like below

{ 
  Name : 'John',
  age : 33,
  tags : {
     skill: [{
         first: '.NET',
         second: 'JAVA',
         third: [{special1:'sleep'},{special2:'eat'}] 
     }]
     skill[3]: {
         third: {special3:'run'}
     }
  }
}

I wish to know if there a way to add to arrays in nested object using dexie-mongoify? Help is appreciated.

akolnati avatar Jul 04 '17 15:07 akolnati

Have you tried to use $push update operator ?

YurySolovyov avatar Jul 04 '17 16:07 YurySolovyov

ah!! Was about to try that but didnt know how do i form the query.

from referring to the below official example... how do i query tags.skill.third ( nested )

db.collection('people').findOne({ skills: { $in: ['html', 'javascript'] }  }, updates).then(function(person) {

    return db.collection('people').update(person, { $push: { skills: 'jquery' } });

}).then(function(updatesCount) {
    // person has jquery skills now
});

i tried below

db.collection('people').findOne({ 'tags.skill.third': { $in: ['html', 'javascript'] }  }, updates).then(function(person) {

    return db.collection('people').update(person, { $push: { skills: 'jquery' } });

}).then(function(updatesCount) {
    // person has jquery skills now
});

but all i get is undefined

akolnati avatar Jul 04 '17 18:07 akolnati

Undefined at which line?

YurySolovyov avatar Jul 04 '17 18:07 YurySolovyov

sorry for the confusion..what i meant was i tried a similar query to test the nested objects and that results in undefined.

for below sample object from people store

{ 
  name : 'John',
  age : 33,
  tags : {
     skill: {
         first: '.NET',
         second: 'JAVA',
     }
  }
}

if i execute the following simple query i get undefined for people[0] meaning the array is empty

db.collection('people').find({'tags.skill.first': { $in : ['.NET']}}).toArray()
    .then((people) => {
      console.log(people); // eslint-disable-line
    });

below query works

db.collection('people').find({'name': { $in : ['John']}}).toArray()
    .then((people) => {
      console.log(people); // eslint-disable-line
    });

Writing the statement like 'tags.skill.first' is supported?

akolnati avatar Jul 04 '17 18:07 akolnati

Writing the statement like 'tags.skill.first' is supported?

No

YurySolovyov avatar Jul 04 '17 18:07 YurySolovyov

Hi Yury, Thanks for the swift updates...Any plans to add such a feature in near future? or a work around that might suffice.. using this addon was such fun so far. Thanks

akolnati avatar Jul 04 '17 19:07 akolnati

I'll try to come up with something, but no promises.

YurySolovyov avatar Jul 04 '17 19:07 YurySolovyov

Brilliant...Thanks

akolnati avatar Jul 04 '17 19:07 akolnati

Hey @OpsSingular, I started work on it. Branch dot-props, nothing works tho. Just FYI

YurySolovyov avatar Jul 24 '17 20:07 YurySolovyov