An operator for mongodb in nodejs.
To view documentation or get support, visit docs.
Note:
w-orm-mongodb
is mainly dependent onmongodb
,saslprep
,events
andstream
.
Note:
saslprep
is used bymongodb
for checking user's password.
npm i w-orm-mongodb
Link: [dev source code]
import wo from 'w-orm-mongodb'
let opt = {
url: 'mongodb://username:[email protected]:27017',
db: 'worm',
cl: 'users',
}
let rs = [
{
id: 'id-peter',
name: 'peter'
},
{
id: 'id-rosemary',
name: 'rosemary'
},
{
id: '',
name: 'kettle'
},
]
let rsm = [
{
id: 'id-peter',
name: 'peter(modify)'
},
{
id: 'id-rosemary',
name: 'rosemary(modify)'
},
{
id: '',
name: 'kettle(modify)'
},
]
async function test() {
//w
let w = wo(opt)
//on
w.on('change', function(mode, data, res) {
console.log('change', mode)
})
//delAll
await w.delAll()
.then(function(msg) {
console.log('delAll then', msg)
})
.catch(function(msg) {
console.log('delAll catch', msg)
})
// => delAll then { n: 2, nDeleted: 2, ok: 1 }
//insert
await w.insert(rs)
.then(function(msg) {
console.log('insert then', msg)
})
.catch(function(msg) {
console.log('insert catch', msg)
})
// => insert then { n: 3, nInserted: 3, ok: 1 }
//save
await w.save(rsm, { autoInsert: false })
.then(function(msg) {
console.log('save then', msg)
})
.catch(function(msg) {
console.log('save catch', msg)
})
// => save then [
// { n: 1, nModified: 1, ok: 1 },
// { n: 1, nModified: 1, ok: 1 },
// { n: 0, nModified: 0, ok: 1 }, //autoInsert=false
// { n: 1, nInserted: 1, ok: 1 } //autoInsert=true
// ]
//select all
let ss = await w.select()
console.log('select all', ss)
// => select all [
// { id: 'id-peter', name: 'peter(modify)', value: 123 },
// { id: 'id-rosemary', name: 'rosemary(modify)', value: 123.456 },
// { id: '{random id}', name: 'kettle', value: 456 },
// { id: '{random id}', name: 'kettle(modify)' } //autoInsert=true
// ]
//select
let so = await w.select({ id: 'id-rosemary' })
// => select [ { id: 'id-rosemary', name: 'rosemary(modify)', value: 123.456 } ]
//select by $and, $gt, $lt
let spa = await w.select({ '$and': [{ value: { '$gt': 123 } }, { value: { '$lt': 200 } }] })
// => select [ { id: 'id-rosemary', name: 'rosemary(modify)', value: 123.456 } ]
//select by $or, $gte, $lte
let spb = await w.select({ '$or': [{ value: { '$lte': -1 } }, { value: { '$gte': 200 } }] })
// => select [ { id: '{random id}', name: 'kettle', value: 456 } ]
//select by $or, $and, $ne, $in, $nin
let spc = await w.select({ '$or': [{ '$and': [{ value: { '$ne': 123 } }, { value: { '$in': [123, 321, 123.456, 456] } }, { value: { '$nin': [456, 654] } }] }, { '$or': [{ value: { '$lte': -1 } }, { value: { '$gte': 400 } }] }] })
console.log('select by $or, $and, $ne, $in, $nin', spc)
// => select [
// { id: 'id-rosemary', name: 'rosemary(modify)', value: 123.456 },
// { id: '{random id}', name: 'kettle', value: 456 }
// ]
//select by regex
let sr = await w.select({ name: { $regex: 'PeT', $options: '$i' } })
// => select [ { id: 'id-peter', name: 'peter(modify)', value: 123 } ]
//del
let d = ss.filter(function(v) {
return v.name === 'kettle'
})
w.del(d)
.then(function(msg) {
console.log('del then', msg)
})
.catch(function(msg) {
console.log('del catch', msg)
})
// => del then [ { n: 1, nDeleted: 1, ok: 1 } ]
}
test()
Link: [dev source code]
import wo from 'w-orm-mongodb'
let opt = {
url: 'mongodb://username:[email protected]:27017',
db: 'worm',
cl: 'usersGfs',
}
async function test() {
//w
let w = wo(opt)
//fn_in, fn_out
let fn_in = 'data(in).dat'
let fn_out = 'data(out).dat'
//u8a, input from file
let b = await fs.readFileSync(fn_in)
let u8a = new Uint8Array(b)
//let u8a = new Uint8Array([66, 97, 115])
console.log('u8a', u8a)
// => u8a Uint8Array [...]
//delAllGfs
await w.delAllGfs()
.then(function(msg) {
console.log('delAllGfs then', msg)
})
.catch(function(msg) {
console.log('delAllGfs catch', msg)
})
// => delAllGfs then { n: 0, ok: 1 }
//insertGfs
let gi = await w.insertGfs(u8a)
console.log('insertGfs', gi)
// => insertGfs { n: 1, ok: 1, id: '{random id}' }
//selectGfs
let gs = await w.selectGfs(gi.id)
console.log('selectGfs', gs)
// => selectGfs Uint8Array [...]
//output
fs.writeFileSync(fn_out, gs)
//delGfs
let gd = await w.delGfs(gi.id)
console.log('delGfs', gd)
// => delGfs { n: 1, nDeleted: 1, ok: 1 }
}
test()