as-mongo
v1.0.1
Published
Manipulate object by static json.
Downloads
2
Maintainers
Readme
As-Mongo
Manipulate object by static json, as mongo.
import asMongo
import { amUpdate } from './as-mongo';
$set
const obj = { prop1: 'prop1', prop2: 'prop2' };
amUpdate(obj, { $set: { prop1: 'good' } });
const result = JSON.stringify(obj);
const goal = '{"prop1":"good","prop2":"prop2"}';
if (result === goal) {
console.log('$set: success');
} else {
console.log('change object: failed');
console.log('hope:', goal);
console.log('but :', result);
}
$push obj
const bePushedArr = [{ prop1: 'obj1' }, { prop1: 'opj2' }];
amUpdate(
{ bePushedArr },
{
$push: {
bePushedArr: { prop1: 'obj3' },
},
},
);
const pushed = JSON.stringify(bePushedArr);
const goalPushed = '[{"prop1":"obj1"},{"prop1":"opj2"},{"prop1":"obj3"}]';
if (pushed === goalPushed) {
console.log('$push: success');
} else {
console.log('$push: failed');
console.log('hope:', goalPushed);
console.log('but :', pushed);
}
$push string
const bePushedArrStr = [{ prop1: 'obj1' }, { prop1: 'opj2' }];
amUpdate(
{ bePushedArrStr },
{
$push: {
bePushedArrStr: 'test',
},
},
);
const pushedStr = JSON.stringify(bePushedArrStr);
const goalPushedStr = '[{"prop1":"obj1"},{"prop1":"opj2"},"test"]';
if (pushedStr === goalPushedStr) {
console.log('$push str: success');
} else {
console.log('$push str: failed');
console.log('hope:', goalPushedStr);
console.log('but :', pushedStr);
}
$addToSet
const beSettedArr = [{ prop1: 'obj1' }, { prop1: 'opj2' }];
amUpdate(
{ beSettedArr },
{
$addToSet: {
beSettedArr: {
$each: [
{ prop1: 'obj1' },
{ prop1: 'obj3' },
],
},
//beSettedArr: { prop1: 'obj3' }
},
},
);
const setted = JSON.stringify(beSettedArr);
const goalSetted = '[{"prop1":"obj1"},{"prop1":"opj2"},{"prop1":"obj3"}]';
if (setted === goalSetted) {
console.log('$addToSet: success');
} else {
console.log('$addToSet: failed');
console.log('hope:', goalSetted);
console.log('but :', setted);
}
$push $each
const beMultiPushedArr = [{ prop1: 'obj1' }, { prop1: 'opj2' }];
amUpdate(
{ beMultiPushedArr },
{
$push: {
beMultiPushedArr: {
$each: [{ prop1: 'obj3'}, { prop2: 'obj4' }]
},
},
},
);
const multiPushed = JSON.stringify(beMultiPushedArr);
const goalMultiPushed = '[{"prop1":"obj1"},{"prop1":"opj2"},{"prop1":"obj3"},{"prop2":"obj4"}]';
if (multiPushed === goalMultiPushed) {
console.log('$push $each: success');
} else {
console.log('$push $each: failed');
console.log('hope:', goalMultiPushed);
console.log('but :', multiPushed);
}
$pull
const pulledArr = [{ prop1: 'obj_1_1' }, { prop1: 'obj_2_1' }, { prop1: 'obj_3_1', prop2: 'obj_3_2' }];
amUpdate(
{
deep: {
pulledArr,
},
},
{
$pull: {
'deep.pulledArr': {
prop1: 'obj_3_1',
},
},
},
);
const pulled = JSON.stringify(pulledArr);
const pulledGoal = '[{"prop1":"obj_1_1"},{"prop1":"obj_2_1"}]';
if (pulled === pulledGoal) {
console.log('$pull: success');
} else {
console.log('$pull: failed');
console.log('hope:', pulledGoal);
console.log('but :', pulled);
}
arrayFilters (for nested array)
const beFiltedArr = [
{
prop1: 'obj_1',
children: [{ prop1: 'obj_1_1' }]
},
{
prop1: 'obj_2',
children: [{ prop1: 'obj_2_1'}]
}
];
amUpdate(
{ beFiltedArr },
{
$push: {
'beFiltedArr[item].children': { prop1: 'obj3' },
},
},
{
arrayFilters: {
item: { prop1: 'obj_2' },
},
},
);
const pushedFiltedArr = JSON.stringify(beFiltedArr);
const goalPushedFiltedArr = '[{"prop1":"obj_1","children":[{"prop1":"obj_1_1"}]},{"prop1":"obj_2","children":[{"prop1":"obj_2_1"},{"prop1":"obj3"}]}]';
if (pushedFiltedArr === goalPushedFiltedArr) {
console.log('arrayFilters: success');
} else {
console.log('arrayFilters: failed');
console.log('hope:', goalPushedFiltedArr);
console.log('but :', pushedFiltedArr);
}