本文介绍了MongoDB:聚合具有静态值的$ project add字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以以某种方式添加具有静态(未计算)值的自定义字段吗?
Can I somehow add custom field with static (not computed) value?
我想在发送之前准备对象,并且需要删除一些带有内部信息的字段并添加带有某些实体ID的字段.
I want to prepare objects before send and I need to remove some fields with internal information and add field with some entity ID.
例如,我有类似这样的对象的测试"集合
For example I have collection "test" with objects like this
{_id: ObjectId(...), data: {...}}
我需要将其转换为
{data: {...}, entity_id: 54}
那么如何在不循环生成代码的情况下添加entity_id:54?
So how can I add entity_id: 54 without looping over result in my code?
db.test.aggregate({ $project: {_id: 0, data: 1, entity_id: ? } })
谢谢
推荐答案
请注意,$ literal是在Mongo 2.6中实现的.所以现在您可以简单地编写:
Note that $literal was implemented in Mongo 2.6.So now you can simply write:
db.test.aggregate(
{$project: {_id: 0, data: 1, entity_id: {$literal: 54}}})
请参见 $ literal .
这篇关于MongoDB:聚合具有静态值的$ project add字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!