npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fk-db

v1.0.1-beta-1.2

Published

mongodb

Downloads

11

Readme

nodejs 封装 mongodb数据库

引入模块

const db = require('fworkMongo')
await db.connect({
    name:'test',
    url:'mongodb://127.0.0.1:27017/',
    model:{ //数据模型 可以为空 针对添加数据和更新数据
        test:{//数据表名 可以为空
            key1: 'String',//数据类型 支持All String、Number、Boolean、Array Object
            key2: 'String'
        }
    }
})

1、插入数据

const res = await db.add('test',data)

1.插入单条数据

const data = {
    "key1": "test1",
    "key2": "test2"
}

添加成功
res = {
	"status": 200,
	"ids": ["62c433c4b083dab65d6cdc89"]
}
添加失败
res = {
	"status": 400,
	"msg": "添加失败!"
}

2.插入多条数据

const data = [
    {
        "key1": "test1",
        "key2": "test2"
    },
    {
        "key3": "test3",
         "key4": "test4"
    }
]

添加成功
res = {
	"status": 200,
	"ids": [
        "62c43637e41ffcb3eb94f711",
        "62c43637e41ffcb3eb94f712"
    ]
}
添加失败
res = {
	"status": 400,
	"msg": "添加失败!"
}

2、删除数据

1.删除多条数据

const data = {
   name: '张三'
}
const res = await db.del('test',data)
const res = await db.remove('test',data)

2.删除单条数据

const res = await db.doc('test').del('6305c5a525350ae1cacd4b41')  //删除
返回成功
res = {
	"status": 200,
	"count": 1  //删除数量
}
返回失败
res = {
	"status": 400,
	"msg": "删除失败!"
}

3、更新数据

1.更新单条数据

const set = {name:'李四'} //更新数据源
const id = '62cd75e6bce3df9417d2714d'
const res = await db.doc('test').upd(id,set)
const res = await db.doc('test').update(id,set)  

2.更新多条数据

const data = {
    where: { _id: '6305c0a66acf306b99b02de0' }, //查找数据源 不得为空或{}
    set: { //更新数据源
        name: '张三',
        ages: 36,
    }
}
const res = await db.update('test',data)
返回成功
res = {
	"status": 200,
	"count": 1  //更新数量
}
返回失败
res = {
	"status": 400,
	"msg": "更新失败!"
}

4、count()根据查询条件获取总条数

const data = {name:'张三'}
const res = await db.count('test',data)
返回成功
res = {
	"status": 200,
	"count": 0  //符合条件总条数
}
返回失败
res = {
	"status": 400,
	"msg": "获取失败!"
}

5、find()条件查询数据

1.根据条件where查询数据

const data = {
    where:{
        "key1": "test1"
    },
    project:{key1:0},//0隐藏 1只返回key1和_id
    skip:0,//跳过指定数目的文档
    sort:{'_id':1},//1升序 -1降序 
    limit:20 //限制20个每页
}
const res = await db.find('test',data)
返回成功
res = {
	"status": 200,
	"count": 1,
	"docs": [
		{
			"_id": "62d00a998f0db7414f0c6285",
			"key1": "test1",
			"key2": "test2"
		}
	]
}
返回失败
res = {
    status: 400,
    msg: '查询失败!'
}

6、聚合查询数据

根据管道查询数据

const data = [
    // {
    //     $lookup: {
    //         from: 'test', // 关联表名
    //         localField: '_id', // 查找表的字段
    //         foreignField: '_id', // 关联表的字段
    //         as: 'key1' // 定义返回数据的字段
    //     }
    // },
    {
        $match:{"key1":"test1"}
    },
    {
        $project:{"key1":0}
    }
]
const res = await db.agg('test',data)
返回成功
res = {
	"status": 200,
	"count": 1,
	"docs": [
		{
			"_id": "62d00a998f0db7414f0c6285",
			"key1": "test1",
			"key2": "test2"
		}
	]
}
返回失败
res = {
    status: 400,
    msg: '查询失败!'
}

7、方法doc 根据id 删除、获取、更新指定文档

1.获取数据

const res = await db.doc('test').get('62d2a65e99d3232cc742c6e0')
返回成功
res = {
	"status": 200,
	"count": 1,
	"docs": [
		{
			"_id": "62d2bd172b39191df29a4db8",
			"key1": "test1",
			"key2": "test2"
		}
	]
}
返回失败
res = {
    status: 400,
    msg: '获取失败!'
}

2.删除数据

const res = await db.doc('test').del('62d2a65e99d3232cc742c6e0')
const res = await db.doc('test').remove('62d2a65e99d3232cc742c6e0')
返回成功
res = {
	"status": 200,
	"count": 1
}
返回失败
res = {
    status: 400,
    msg: '删除失败!'
}

3.更新数据

const res = await db.doc('test').upd('62d2a65e99d3232cc742c6e0',{name:李四})
const res = await db.doc('test').update('62d2a65e99d3232cc742c6e0',{name:李四})
返回成功
res = {
	"status": 200,
	"count": 1
}
返回失败
res = {
    status: 400,
    msg: '更新失败!'
}