- Reference >
- Operators >
- Aggregation Pipeline Operators >
- $setField (aggregation)
$setField (aggregation)¶
On this page
Definition¶
Syntax¶
$setField
has the following syntax:
You must provide the following fields:
Field | Type | Description |
---|---|---|
field |
String | Field in the input object that you want to add, update, or
remove. field can be any valid expression that resolves to a string
constant. |
input |
Object | Document that contains the field that you want to add or
update. input must resolve to an object, missing ,
null , or undefined . |
value |
Expression | The value that you want to assign to Set to |
Behavior¶
If
input
evaluates tomissing
,undefined
, ornull
,$setField
returnsnull
and does not updateinput
.If
input
evaluates to anything other than an object,missing
,undefined
, ornull
,$setField
returns an error.If
field
resolves to anything other than a string constant,$setField
returns an error.If
field
doesn’t exist ininput
,$setField
adds it.$setField
doesn’t implicitly traverse objects or arrays. For example,$setField
evaluates afield
value of"a.b.c"
as a top-level field"a.b.c"
instead of as a nested field,{ "a": { "b": { "c": } } }
.$unsetField
is an alias for$setField
with an input value of$$REMOVE
. The following expressions are equivalent:
See also
Examples¶
Add Fields that Contain Periods (.
)¶
Consider an inventory
collection with the following documents:
The following operation uses the $replaceWith
pipeline stage
and the $setField
operator to add a new field to each
document, "price.usd"
. The value of "price.usd"
will equal the
value of "price"
in each document. Finally, the operation uses the
$unset
pipeline stage to remove the "price"
field.
The operation returns the following results:
Add Fields that Start with a Dollar Sign ($
)¶
Consider an inventory
collection with the following documents:
The following operation uses the $replaceWith
pipeline
stage and the $setField
and $literal
operators to add a new field to each document, "$price"
. The value
of "$price"
will equal the value of "price"
in each document.
Finally, the operation uses the $unset
pipeline stage to
remove the "price"
field.
The operation returns the following results:
Update Fields that Contain Periods (.
)¶
Consider an inventory
collection with the following documents:
The following operation uses the $match
pipeline stage to
find a specific document and the $replaceWith
pipeline stage
and the $setField
operator to update the "price.usd"
field in the matching document:
The operation returns the following results:
Update Fields that Start with a Dollar Sign ($
)¶
Consider an inventory
collection with the following documents:
The following operation uses the $match
pipeline stage to
find a specific document and the $replaceWith
pipeline stage
and the $setField
and $literal
operators to
update the "$price"
field in the matching document:
The operation returns the following results:
Remove Fields that Contain Periods (.
)¶
Consider an inventory
collection with the following documents:
The following operation uses the $replaceWith
pipeline stage
and the $setField
operator and $$REMOVE
to remove the "price.usd"
field from each document:
The operation returns the following results:
A similar query written using the $unsetField
alias
returns the same results:
Remove Fields that Start with a Dollar Sign ($
)¶
Consider an inventory
collection with the following documents:
The following operation uses the $replaceWith
pipeline
stage, the $setField
and $literal
operators,
and $$REMOVE
to remove the "$price"
field
from each document:
The operation returns the following results:
A similar query written using the $unsetField
alias
returns the same results:
See also