- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章mongoDB使用投影剔除‘额外’字段的操作过程由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
。
。
实际开发过程中,为便于开发人员定位问题,常存在多个额外的字段。例如:增加createdAt、updatedAt字段以查看数据的创建和更改时间。而对于客户端而言,无需知道其存在。针对以上情况,本文详细介绍了“额外”字段的用途以及处理过程.
技术栈 。
。
。
。
1.1 "额外"是指与业务无关 。
mongodb中,collection中存储的字段并不仅仅有业务字段。有些情况下,会存储多余的字段,以便于开发人员定位问题、扩展集合等.
额外的含义是指 和业务无关、和开发相关的字段。这些字段不需要被用户所了解,但是在开发过程中是至关重要的.
。
1.2 产生原因 。
产生额外字段的原因是多种多样的.
。
。
额外字段的产生原因有很多,可以以此进行分类.
。
2.1 _id、__v字段 。
产生原因:以mongoose为例,通过schema->model->entity向mongodb中插入数据时,该数据会默认的增加_id、__v字段.
_id字段是由mongodb默认生成的,用于文档的唯一索引。类型是ObjectID。mongoDB文档定义如下:
“ MongoDB creates a unique index on the _id field during the creation of a collection. The _id index prevents clients from inserting two documents with the same value for the _id field. You cannot drop this index on the _id field.< 。
__v字段是由mongoose首次创建时默认生成,表示该条doc的内部版本号.
“ The versionKey is a property set on each document when first created by Mongoose. This keys value contains the internal revision of the document. The versionKey option is a string that represents the path to use for versioning. The default is __v. 。
。
2.2 createdAt、updatedAt字段 。
createdAt、updatedAt字段是通过timestamp选项指定的,类型为Date.
“ The timestamps option tells mongoose to assign createdAt and updatedAt fields to your schema. The type assigned is Date.By default, the names of the fields are createdAt and updatedAt. Customize the field names by setting timestamps.createdAt and timestamps.updatedAt. 。
。
2.3 is_deleted字段 。
is_deleted字段是实现软删除一种常用的方式。在实际业务中,出于各种原因(如删除后用户要求再次恢复等),往往采用的软删除,而非物理删除.
因此,is_deleted字段保存当前doc的状态。is_deleted字段为true时,表示当前记录有效。is_deleted字段为false时,表示当前记录已被删除.
。
。
。
3.1 额外字段生成 。
_id字段是必选项;__v、createdAt、updatedAt字段是可配置的;status字段直接加在s对应的chema中。相关的schema代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
|
isdeleted: {
type: String,
default
:
true
,
enum
: [
true
,
false
],
},
id: {
type: String,
index:
true
,
unqiue:
true
,
default
:uuid.v4(),
}},
{timestamps:{createdAt:
'docCreatedAt'
,updatedAt:
"docUpdatedAt"
},versionKey:
false
});
|
通过配置schema中的timestamps选项,可以将createdAt和updatedAt字段加入到doc中。在创建和更新doc时,这两个字段无需传入,就会自动变化.
。
3.2 额外字段清理 。
通过3.1可以明确的产生若干额外字段,但是客户端调用接口并返回时,这些字段是无需得知的。因此需对额外字段进行清理。清理方式分为投影和过滤.
以query、update接口为例。其中query接口用于:1、查询指定字段 2、查询全部字段 3、分页排序查询。update接口用于更新并返回更新后的数据.
根据是否需要指定字段、和collection中有无内嵌的情况划分,一共有4类。接着针对这4种情况进行分析.
1、有指定字段、无内嵌 。
2、无指定字段、无内嵌 。
3、有指定字段、有内嵌 。
4、无指定字段、有内嵌 。
。
3.2.1 投影 。
有指定字段是指在查询时指定查询字段,而无需全部返回。mongo中实现指定的方式是投影 (project) 。mongo官方文档中定义如下:
“ The $project takes a document that can specify the inclusion of fields, the suppression of the _id field, the addition of new fields, and the resetting of the values of existing fields. Alternatively, you may specify the exclusion of fields. 。
$project可以做3件事
1.指定包含的字段、禁止_id字段、添加新字段 。
2.重置已存在字段的值 。
3.指定排除的字段 。
我们只需关注事情1、事情3。接着查看mongoose中对project的说明:
“ When using string syntax, prefixing a path with - will flag that path as excluded. When a path does not have the - prefix, it is included. Lastly, if a path is prefixed with +, it forces inclusion of the path, which is useful for paths excluded at the schema level. 。
A projection must be either inclusive or exclusive. In other words, you must either list the fields to include (which excludes all others), or list the fields to exclude (which implies all other fields are included). The _id field is the only exception because MongoDB includes it by default. 。
注意:此处指query "projection" 。
mongoose表明:投影要么是全包含,要么是全剔除。不允许包含和剔除同时存在。但由于 。
_id是MongoDB默认包含的,因此_id是个例外.
select project投影语句组装代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
/**
* 添加通用筛选条件
* @param {*} stat 已装配的筛选语句
* @param {*} collection collectionName
* @return {*} 组装完成的语句
*/
function addCommonSelectCond(stat,collection)
{
if
(typeof(stat)!=
"object"
)
return
;
stat[
"_id"
] =
0
;
stat[
"__v"
] =
0
;
stat[
"status"
] =
0
;
stat[
"docCreatedAt"
] =
0
;
stat[
"docUpdatedAt"
] =
0
;
var embeddedRes = hasEmbedded(collection);
if
(embeddedRes[
"isEmbedded"
])
{
for
(var item of embeddedRes[
"embeddedSchema"
])
{
stat[item+
"._id"
] =
0
;
stat[item+
".__v"
] =
0
;
stat[item+
".status"
] =
0
;
stat[item+
".docCreatedAt"
] =
0
;
stat[item+
".docUpdatedAt"
] =
0
;
}
}
return
stat;
}
|
。
3.2.2 过滤 。
通过findOneAndupdate、insert、query等返回的doc对象中(已经过lean或者toObject处理),是数据库中真实状态。因此需要对产生的doc进行过滤,包括doc过滤和内嵌文档过滤.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
/**
* 处理自身及内嵌的表
* @param {*} collection 查询的表
* @param {*} doc 已查询出的结果
* @returns doc 清理后的结果
*/
static
clearDoc(collection,doc){
if
(doc === undefined || doc ===
null
|| typeof doc !=
"object"
)
return
null
;
doc =
this
.clearExtraField(doc);
var res = hasEmbedded(collection);
if
(res[
"isEmbedded"
])
{
let arr = res[
"embeddedSchema"
];
for
(var item of arr){
if
(doc[item])
doc[item] =
this
.clearArray(doc[item]);
}
}
return
doc;
}
static
clearExtraField(doc){
if
(doc ===
null
|| typeof doc !=
"object"
)
return
;
var del = delete doc[
"docCreatedAt"
]&&
delete doc[
"docUpdatedAt"
]&&
delete doc[
"_id"
]&&
delete doc[
"__v"
]&&
delete doc[
"status"
];
if
(!del)
return
new
Error(
"删除额外字段出错"
);
return
doc;
}
static
clearArray(arr)
{
if
(!Array.isArray(arr))
return
;
var clearRes =
new
Array();
for
(var item of arr){
clearRes.push(
this
.clearExtraField(item));
}
return
clearRes;
}
|
细心的读者已经发现了,投影和过滤的字段内容都是额外字段。那什么情况下使用投影,什么情况下使用过滤呢?
关于这个问题,笔者的建议是如果不能确保额外字段被剔除掉,那就采取双重认证:查询前使用投影,查询后使用过滤.
。
4 总结 。
本文介绍了实际业务中往往会产生额外字段。而在mongoDB中,"消除"额外字段的手段主要是投影、过滤.
以使用频率最高的查询接口为例,整理如下:
。
指定选项 | 内嵌选项 | 查询前投影 | 查询后过滤 |
---|---|---|---|
有指定 | 无内嵌 | × | √ |
有指定 | 有内嵌 | × | √ |
无指定 | 无内嵌 | √ | × |
无指定 | 有内嵌 | √ | × |
。
因此,笔者建议无论schema中是否配置了options,在查询时组装投影语句,查询后进行结果过滤。这样保证万无一失, 。
额外字段才不会漏到客户端**.
到此这篇关于mongoDB使用投影剔除‘额外'字段的文章就介绍到这了,更多相关mongoDB用投影剔除额外字段内容请搜索我以前的文章或继续浏览下面的相关文章希望大家以后多多支持我! 。
原文链接:https://juejin.cn/post/6915767241640247309 。
最后此篇关于mongoDB使用投影剔除‘额外’字段的操作过程的文章就讲到这里了,如果你想了解更多关于mongoDB使用投影剔除‘额外’字段的操作过程的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
在有些场景下,我们需要对我们的varchar类型的字段做修改,而修改的结果为两个字段的拼接或者一个字段+字符串的拼接。 如下所示,我们希望将xx_role表中的name修改为name+id。
SELECT incMonth as Month, SUM( IF(item_type IN('typ1', 'typ2') AND incMonth = Month, 1, 0 ) )AS
我最近读到 volatile 字段是线程安全的,因为 When we use volatile keyword with a variable, all the threads read its va
我在一些模型中添加了一个 UUID 字段,然后使用 South 进行了迁移。我创建的任何新对象都正确填充了 UUID 字段。但是,我所有旧数据的 UUID 字段为空。 有没有办法为现有数据填充 UUI
刚刚将我的网站从 mysql_ 更新为 mysqli,并破坏了之前正常运行的查询。 我试图从旋转中提取 id,因为它每次都会增加 1,但我不断获取玩家 id,有人可以告诉我我做错了什么吗?我尝试了将
我在 Mac OS X 上使用带有 Sequel Pro 的 MySQL。我想将一个表中的一个字段(即名为“GAME_DY”的列)复制到另一个名为“DAY_ID”的表的空字段中。两个表都是同一数据库的
问题: 是否有可能有一个字段被 JPA 保留但被序列化跳过? 可以实现相反的效果(JPA 跳过字段而序列化则不会),如果使用此功能,那么相反的操作肯定会很有用。 类似这样的事情: @Entity cl
假设我有一个名为“dp”的表 Year | Month | Payment| Payer_ID | Payment_Recipient | 2008/2009 | July
我将尝试通过我的 Raspberry Pi 接入点保证一些 QoS。 开始之前,我先动手:我阅读了有关 tcp、udp 和 ip header 的内容。在IP header description我看
如果你能弄清楚如何重命名这个问题,我愿意接受建议。 在 Dart 语言中,可以编写一个带有 final 字段的类。这些是只能设置的字段构造函数前 body 跑。这可以在声明中(通常用于类中的静态常量)
你怎么样? 我有两个带有两个字段的日期选择器 我希望当用户选择 (From) 时,第二个字段 (TO) 将是 next day 。比如 booking.com 例如:当用户选择From 01-01-2
我想我已经看到了这个问题的一些答案,这些答案可能与我需要的相差不远,但我对 mysql 的了解还不够确定,所以我会根据我的具体情况提出问题。 我有一个包含多个表的数据库,为此,如果“image”表上的
我在 mySQL 数据库中有 2 个表: customers ============ customer_id (1, 2 ) customer_name (john, mark) orders ==
我正在开发一个员工目标 Web 应用程序。 领导/经理在与团队成员讨论后为他们设定目标。这是一年/半年/季度,具体取决于组织遵循的评估周期。 现在的问题是添加基于时间段的字段或存档上一季度/年度数据的
我正在寻找允许内容编辑器从媒体库中选择多个文件的东西,这些文件将在渲染中列出。他们还需要能够上传文件和搜索。它必须在页面编辑器(版本 8 中称为体验编辑器)中工作。 到目前为止我所考虑的: 一堆文件字
现在,我有以下由 original.df %.% group_by(Category) %.% tally() %.% arrange(desc(n)) 创建的 data.frame。 DF 5),
我想知道是否有一些步骤/解决方案可以处理错误消息并将它们放入 Pentaho 工具中的某个字符串或字段中?例如,如果连接到数据库时发生某些错误,则将该消息从登录到字符串/字段。 最佳答案 我们在作业的
如何制作像短信应用程序一样的“收件人”字段?例如,右侧有一个“+”按钮,当添加某人时,名称将突出显示并可单击,如圆角矩形等。有没有内置的框架? 最佳答案 不,但请参阅 Three20 的 TTMess
是否可以获取记录的元素或字段的列表 通过类型信息类似于类的已发布属性的列表吗? 谢谢 ! 最佳答案 取决于您的delphi版本,如果您使用的是delphi 2010或更高版本,则可以使用“新rtti”
我正在构建一个 SQLite 数据库来保存我的房地产经纪人的列表。我已经能够使用外键来识别每个代理的列表,但我想在每个代理的记录中创建一个列表;从代理商和列表之间的一对一关系转变为一对多关系。 看这里
我是一名优秀的程序员,十分优秀!