
mongodb
曹天骄
这个作者很懒,什么都没留下…
展开
-
mongoose只更新数组中某一项的字段
只是需要一个特殊符号$代表匹配某一项数据库设计如下:核心代码如下: // 回答试题 public async userPaperAnswer(request: IRequest, _h: IResponse) { const { user_paper_id, question_id, option_user } = request.payload; const updOne = await UserPaper.updateOne( { _id: user_paper原创 2021-04-10 21:05:44 · 1220 阅读 · 0 评论 -
mongoose查找若存在,则什么都不做,若不存在,则插入
直接上代码:ModelSchema.findOneAndUpdate( {id: searchId}, { $setOnInsert: { id: searchId, foo: foo, bar: bar, }}, { upsert: true }).catch(error => console.error(error));原创 2021-01-03 15:03:02 · 911 阅读 · 0 评论 -
mongodb在aggregate lookup 进行分页查询,获得记录总数
直接上代码: const ones = await InspectTaskUser.aggregate([{ $facet: { paginatedResult: [ { $match: { user_id: ObjectId(_id) } }, { $skip: (page - 1) * size }, { $limit: size }, { $lookup: {原创 2020-09-16 18:59:10 · 3146 阅读 · 0 评论 -
使用run-rs启动mongodb
为什么需要:因为在mac上直接使用社区版,进行备份数据导入时,会报错:MongoError: Transaction numbers are only allowed on a replica set member or mongosrun-rs可以零配置启动mongodb1、npm install run-rs -g2、run-rs -v 4.0.0 --shell...原创 2020-07-20 20:58:53 · 827 阅读 · 0 评论 -
mac 通过 homebrew 安装mongodb
通过homebrew安装mongodb非常省事,但是如果没有科学上网,可能比较慢,下面是官方安装教程链接:https://github.com/mongodb/homebrew-brew主要步骤1、brew tap mongodb/brew2、brew install mongodb-community3、启动 brew services start mongodb-community4、关闭 brew services stop mongodb-community...原创 2020-06-29 10:13:59 · 589 阅读 · 0 评论 -
我的mongoose代码备份
const users = ones.docs.map(x => { return new ObjectId(x.id); }) const userInDepart = await UserDepart.aggregate([ { $match: { 'user': { $in: users } } }, { $group: { ...原创 2019-03-21 14:16:41 · 215 阅读 · 0 评论 -
mongoose更新数据,如果这条记录不存在,则直接变为新增
主要用到findOneAndUpdatelet query = { /* query */ };let update = {expire: new Date()};let options = {upsert: true, new: true, setDefaultsOnInsert: true};let model = await Model.findOneAndUpdate(query,...原创 2019-03-15 14:53:17 · 4990 阅读 · 0 评论 -
centos 宝塔面板 mongodb 给新的数据库 创建登录用户和密码
之前写过一篇冷环境下mongodb从零开始创建用户密码的文章。https://blog.youkuaiyun.com/solocao/article/details/83347658当我们需要再新增一个账号时,用下面的方法,和第一篇大同小异:1、进入mongodb安装目录,下面是宝塔面板的默认目录cd /www/server/mongodb/bin2、输入命令行mongo,进入mongodb环境...原创 2019-03-05 09:30:33 · 2064 阅读 · 0 评论 -
mongoose 在数组中添加数据
主要使用$pushPersonModel.update( { _id: person._id }, { $push: { friends: friend } })原创 2019-02-09 12:27:05 · 3535 阅读 · 1 评论 -
typescript mongodb 教程搜集
https://tutorialedge.net/typescript/typescript-mongodb-beginners-tutorial/原创 2019-01-28 18:46:28 · 1034 阅读 · 0 评论 -
阿里云轻应用服务器 宝塔面板 mongodb 配置外网连接 其一 基础配置
买了一台阿里云轻应用服务器做测试,我默认安装的是宝塔面板,下面详细记录配置mongodb过程。1、进行安装,通过宝塔面板自带的软件管理进行安装2、首先需要保证外网能够进行访问设置监听端口为0.0.0.0,原来的是127.0.0.1只允许本地访问,现在变成允许外网访问在【安全】配置项中,放行27017端口,此时会显示状态外网不通。在阿里云【管理控制台】中的防火墙中添加规则,允许270...原创 2019-04-06 11:32:00 · 2172 阅读 · 0 评论 -
阿里云轻应用服务器 宝塔面板 mongodb 配置外网连接 其二 配置账号密码
其一的设置,不需要通过账号密码,就能直接访问数据库,安全性堪忧,我之前就踩过坑,也在之前的文章中讲过,这次用阿里云轻应用服务器重新介绍一波。命令行窗口可以直接通过阿里云轻应用服务器里面自带的。1、进入mongodb安装目录,下面是宝塔面板的默认目录cd /www/server/mongodb/bin2、输入命令行mongo,进入mongodb环境mongo3、切换到 admin 数...原创 2019-04-06 13:50:20 · 945 阅读 · 0 评论 -
mongodb save和insert区别
mongodb的save和insert函数都可以向collection里插入数据,但两者是有两个区别:一、使用save函数里,如果原来的对象不存在,那他们都可以向collection里插入数据,如果已经存在,save会调用update更新里面的记录,而insert则会忽略操作二、insert可以一次性插入一个列表,而不用遍历,效率高, save则需要遍历列表,一个个插入。...原创 2019-05-13 19:56:23 · 1710 阅读 · 0 评论 -
Mongoose provide access to previous value of property in pre('save')
Mongoose allows you to configure custom setters in which you do the comparison. pre(‘save’) by itself won’t give you what you need, but together:schema.path('name').set(function (newVal) { var orig...转载 2019-06-16 20:10:34 · 225 阅读 · 0 评论 -
mongodb [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify
今天重启电脑,跑mongodb遇到报错:[main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'直接写我的解决方法:可以先看看mongodb进行占用情况ps -ef|grep mongod然后直接干掉mongosudo pkill mon...原创 2019-06-28 20:51:21 · 12949 阅读 · 0 评论 -
mongoose获取最高分
我的需求:获取所有成绩中的最高分。方法:根据分数倒序,查询第一个。 const topScore = await Score.find().sort({ score: -1 }).skip(0).limit(1);原创 2019-08-06 15:31:58 · 202 阅读 · 0 评论 -
mongodb $ifNull
$ifNull用于判断第一个表达式是否为 NULL,如果为 NULL 则返回第二个参数的值,如果不为 NULL 则返回第一个参数的值。$ifNull表达式格式为:{ $ifNull: [ <expression>, <replacement-expression-if-null> ] }例子:原始数据:{ "_id" : 1, "item" : "abc1", ...原创 2019-09-01 10:47:32 · 4083 阅读 · 0 评论 -
mongoose findOneAndUpdate 查找更新后的数据{ new: true }
需要增加一个配置项 { new: true },则可以查找后面的内容。const updOne = await Verify.findOneAndUpdate({ _id: verify_id }, { $set: { // 认证通过,状态设置为1 state: 1, // 审核操作人 verify_user: user_id, verify_at: ...原创 2019-01-15 11:06:52 · 8015 阅读 · 0 评论 -
mongoose 多条件 模糊查询
需要知道两个标识符就可以搞定。$or 用于多条件查询 http://www.nodeclass.com/api/mongoose.html#query_Query-or$regex 用于模糊查询 http://www.nodeclass.com/api/mongoose.html#query_Query-regex试例代码://从URL中传来的 keyword参数const keywor...原创 2018-10-05 15:29:29 · 2895 阅读 · 0 评论 -
mongoose日期 时间 范围查询
需要知道的两个修饰符 $gte和$lt$gte (greater-than)$lt (less-than)写法如下const start = new Date(2018, 3, 1);const end = new Date(2018, 4, 1);db.posts.find({created_on: {$gte: start, $lt: end}});...原创 2018-10-05 17:24:05 · 8006 阅读 · 0 评论 -
mongoose 查询 find 指定字段
在第二个参数中填写字符串,需要查询的字段用空格相连。const Person = mongoose.model('Person', yourSchema);// 查询每个 last name 是 'Ghost' 的 person, select `name` 和 `occupation` 字段Person.findOne({ 'name.last': 'Ghost' }, 'name occ...原创 2018-09-29 16:07:24 · 10296 阅读 · 0 评论 -
mongodb 高级查询 统计记录条数
使用count()方法查询表中的记录条数,例如,下面的命令查询表users的记录数量:db.users.find().count();原创 2018-10-15 22:34:31 · 35918 阅读 · 3 评论 -
mongoose 定义经纬度数据类型
本来想用Double在mongoose中直接定义经纬度的。但是发现mongoose的数据类型只有下面几种。StringNumberDateBufferBooleanMixedObjectIdArrayDecimal128Map所以我们使用Decimal128来定义经纬度const LocationsSchema = new mongoose.Schema({ lati...原创 2018-10-17 09:21:58 · 1014 阅读 · 0 评论 -
mac mongodb 安装
1、进入目录cd /usr/local2、下载对应版本sudo curl -O https://fastdl.mongodb.org/osx/mongodb-osx-ssl-x86_64-4.0.3.tgz3、解压到当前目录sudo tar -zxvf mongodb-osx-x86_64-3.4.2.tgz4、重命名为mongodb,主要是方便管理sudo mv mongod...原创 2018-10-25 21:42:21 · 196 阅读 · 0 评论 -
mac安装mongodb
1、去官网进行下载mongodb官网2、将下载好的压缩包解压,将解压出的文件夹下的内容全部复制到新的路径下。cp -r mongodb-osx-x86_64-4.0.4 /usr/local/mongodb3、在新建立的文件夹下建立data文件夹用来记录数据,log文件夹用来记录日志cd /usr/local/mongodbsudo mkdir datasudo mkdir l...原创 2018-11-10 20:39:03 · 465 阅读 · 0 评论 -
mongodb哪些情况适用内嵌,哪些情况适用引用
在 SQL 中,我们经常会提起:一对一,一对多,多对多,而在 MongoDB 这样的数据库中,我们可以分为新的类型:少和多,之后我们会根据少和多进行一些数据库设计的详细分析,先来简单根据之前的介绍引用一下《MongoDB 权威指南》中的表格:更适合内嵌更适合引用子文档较小子文档较大数据不会定期改变数据经常改变最终数据一致即可中间阶段的数据必须一致文档数...转载 2018-11-15 16:48:39 · 582 阅读 · 1 评论 -
mongodb mongoose 常用操作符号 整理
操作符描述$eq等于$or或关系$nor或关系取反$gt大于$gte大于等于$lt小于$lte小于等于$ne 不等于$in在多个值范围内$nin不在多个值范围内$all匹配数组中多个值$regex正则,用于模糊查询$size匹配数组大小$maxDistance...原创 2018-11-21 15:06:02 · 2038 阅读 · 0 评论 -
车型数据库设计 mongodb
直接上代码了const mongoose = require('mongoose');const Schema = mongoose.Schema;// 汽车索引列表const CarListSchema = new mongoose.Schema({ // 首字母 A、B、C... initial: String, // 汽车品牌分类 category: [{ ...原创 2018-12-07 10:46:26 · 779 阅读 · 2 评论 -
mongoose 更新元素 DeprecationWarning: collection.update is deprecated. Use updateOne, updateMany
我一开始的写法:const updOne = await this.update({ _id: verify_id }, { $set: { // 认证通过,状态设置为1 state: 1, // 审核操作人 verify_user, verify_at: Date.now() }});使用mongoose更新元素值,报错了Deprecati...原创 2018-12-14 15:01:16 · 6229 阅读 · 1 评论 -
mongodb $unwind 聚合管道
$unwind:将文档中的某一个数组类型字段拆分成多条,每条包含数组中的一个值。需求:{ "_id" : ObjectId("5951c5de567ebff0d5011fba"), "name" : "陈晓婵", "address" : "北京朝阳区", &a转载 2018-12-14 20:32:01 · 7031 阅读 · 0 评论 -
mongoose 数据库设计千万要注意 Cast to [number] failed for value
不要定义字段type balance_flow: [{ // 提现金额 money: { type: Number }, // 资金类型 0=>提现 withdraw 1=>资金流入 user_active type: Number }],这样会报错...原创 2019-01-10 13:40:45 · 2266 阅读 · 0 评论 -
mongoose $sum
_id:...Segment:[ { field: "S1", value: 1 }, { field: "S2", value: 5 }, { field: "Sn", value: 10 },]查询方式db.collection.aggregate( [ { $unwind: "$Segment" }, { $group: { ...原创 2019-01-12 19:49:22 · 1255 阅读 · 0 评论 -
mongoose 分页查询
使用插件mongoose-paginate进行分页查询,这个库已经2年不更新了,但下载量还是比较大的。地址https://www.npmjs.com/package/mongoose-paginate列举开发过程中常用的代码片段:1、查询某个字段 使用select: 'title date author'var query = {};var options = { selec...原创 2018-10-01 08:56:34 · 1971 阅读 · 0 评论