decay your mongodb to piece "mini-mongodb" with filter of field in schema which you set up before
npm install cky-mongo-decaycky-mongo-decay is a tool that helps you to split your mongodb into smaller mongodb to serve the needs of fragmented storage.
Use environment DEBUG=DecayMongo for show log debug
> All bug or issue please send to email: Chickyky@gmail.com
your special conditionmongoose package for upsert document into your pieces mongodb``sh`
$ npm install cky-mongo-decay --savefnDecay$3
* contructor (options)
`
* example:
js`
fnDecay: (obj) => {
return moment(obj.publishDate).utcOffset(420).format('YYYYMM');
}
pieces mongodb
schema
modelName
piecesOfDecay with fnDecay
* key: result from , which you want your document was filteredpiece mongodb
* value: Connection string of your `
* example:
async
piecesOfDecay: {
'201907': 'mongodb://user:pwd@host:port/db1',
'201908': 'mongodb://user:pwd@host:port/db2',
'201909': 'mongodb://user:pwd@host:port/db3',
'201910': 'mongodb://user:pwd@host:port/db1'
}
limitDecay: ), default is 5pieces mongodb
stopDecayWhenError then set it is false, default is true (Stop and return error callback when error happen)(err, result)
* init (callback)
* Run first for init this lib, it will process connect all connection of your peices mongodb, callback when done with format `
* callback: error when one of host connect fail
* after init success, you can use this lib
* decay (findObj, arrObjDecay, callback)
* findObj : use for query upsert,
* example:
`
{
slug: 1
}
slug
* this example above mean: query key with value of document you want upsert into your peice mongodb, it will become:`
`
{
slug: 'this-is-slug-for-find-query-of-mongodb'
}
peice mongodb
* arrObjDecay , if you pass Array then result in callback is Array[String] else is String`
* example:
`
{
"slug" : "this-is-slug-for-find-query-of-mongodb",
"updatedAt" : new Date("2019-11-21T16:55:00.794+07:00"),
"createdAt" : new Date("2019-11-21T12:19:02.454+07:00"),
"title" : "Hello world!",
"publishDate" : new Date("2019-11-27T14:00:00.000+07:00")
}
(err, result)
* callback stopDecayWhenError
* err: error when process fail and flag is truearrObjDecay
* result: is Array[Object] if is Array else Object. Element in Array is object have field decayId is new ObjectId mongodb, in case you use stopDecayWhenError = false and upsert error then decayId is null.`
* example:
js`
[
{
"find": {
"slug": "slug-1"
},
"decayId": "5dd83b2bd82b317c61758d61"
},
{
"find": {
"slug": "slug-2"
},
"decayId": null
},
]
// or
{
"find": {
"slug": "slug-2"
},
"decayId": "5dd83b2bd82b317c61758d61"
}
// or
{
"find": {
"slug": "slug-3"
},
"decayId": null
}
peices mongodb
* close (callback)
* Close all connection of your you was setup when init
* callback is function (err), err != null when close error
javascript
const moment = require('moment');
const DecayMongo = require('cky-mongo-decay');const decay = new DecayMongo({
fnDecay: (obj) => {
return moment(obj.publishDate).utcOffset(420).format('YYYYMM');
},
schema: require('./FeedSchema'),
modelName: 'Feed',
limitDecay: 1,
stopDecayWhenError: false,
piecesOfDecay: {
'201907': 'mongodb://user:pwd@host:port/db1',
'201908': 'mongodb://user:pwd@host:port/db2',
'201909': 'mongodb://user:pwd@host:port/db3',
'201910': 'mongodb://user:pwd@host:port/db1'
}
});
let obj4Decay = [{
"slug" : "slug-1",
"title" : "Hello world!",
"publishDate" : new Date("2019-07-27T14:00:00.000+07:00")
}, {
"slug" : "slug-2",
"title" : "Hello world!",
"publishDate" : new Date("2019-08-27T14:00:00.000+07:00")
}, {
"slug" : "slug-3",
"title" : "Hello world!",
"publishDate" : new Date("2019-09-27T14:00:00.000+07:00")
}, {
"slug" : "slug-4",
"title" : "Hello world!",
"publishDate" : new Date("2019-10-27T14:00:00.000+07:00")
}, {
"slug" : "slug-5",
"title" : "Hello world!",
"publishDate" : new Date("2019-11-27T14:00:00.000+07:00")
}]
decay.init((err, result) => {
if (err) {
return console.log('init fail, err=', err, 'result=', result);
}
decay.decay({
slug: 1
},
obj4Decay,
(err, result) => {
console.log('done err=', err);
console.log('done result=', result);
decay.close(() => {
process.exit(0);
})
})
})
`result will like this:
`json
[
{
"find": {
"slug": "slug-1"
},
"decayId": "5dd83b2bd82b317c61758d61"
},
{
"find": {
"slug": "slug-2"
},
"decayId": "5dd83b2bd82b317c61758df8"
},
{
"find": {
"slug": "slug-3"
},
"decayId": "5dd83b2cd82b317c61758ea5"
},
{
"find": {
"slug": "slug-4"
},
"decayId": "5dd83b2cd82b317c61758ea5"
},
{
"find": {
"slug": "slug-5"
},
"decayId": null
}
]
``ISC
##### - Chickyky -