YurySolovyov / dexie-mongoify

Dexie.js plugin with MongoDB-like API
MIT License
25 stars 6 forks source link

how to push to array in a nested object #17

Open akolnati opened 7 years ago

akolnati commented 7 years ago

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.

YurySolovyov commented 7 years ago

Have you tried to use $push update operator ?

akolnati commented 7 years ago

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

YurySolovyov commented 7 years ago

Undefined at which line?

akolnati commented 7 years ago

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?

YurySolovyov commented 7 years ago

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

No

akolnati commented 7 years ago

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

YurySolovyov commented 7 years ago

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

akolnati commented 7 years ago

Brilliant...Thanks

YurySolovyov commented 7 years ago

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