save data in array nested on database nosql - Total.js

294 Views Asked by At

i have to save a json in array but i can't in

controllers/api.js

exports.install = function() {

    F.cors('/api/db/*',['post'],false)

    F.route('/api/db/',saveNoSql,['post']);
};


function saveNoSql(){
    console.log('dentro saveNoSql');
    var self = this;
    var body = self.req.body;

    var users = NOSQL('db');
    users.find().make(function(builder){
        builder.and()
        builder.where('name','Report');
        builder.where('entries['+0+'].name','Report');
        builder.callback(function(err,model){

        })
    })
}

databases/db.nosql

{"name":"Report","user":"rep","password":"admin","odata":"false","entries":[{"name":"Report","appointment":[{"idOrder":"1","order":"order 1","supplier":"fornitore 1","hours":"numero ore","actions":"icone azioni"}]},{"name":"Admin","appointment":[]}]}
{"name":"Admin","user":"adm","password":"admin","odata":"true","entries":[]}
{"name":"Mike","user":"mike","password":"admin","odata":"true","entries":[]}

Here you see that i have to save a req.body in name->Report/entries->Report/appointment and i guess to do that with find() and insert() , right?

1

There are 1 best solutions below

2
On

This is solution, but not much effective:

var users = NOSQL('db');

users.find().filter(doc => doc.name === 'Report' && doc.entries && doc.entries[0] && doc.entries[0].name === 'Report').callback(function(err, model) {
    console.log(err, model);
});

I recommend to update all documents by adding new filter fields for much simpler filtering. Documentation: https://docs.totaljs.com/latest/en.html#api~DatabaseBuilder~builder.filter