Mongodb Session 7
Mongodb Session 7
db.tags.update(
{ _id: 1 },
{ $max: { dateExpired: new Date("2013-09-30") } }
)
The operation does not update the dateExpired field:
{
_id: 1,
desc: "decorative arts",
dateEntered: ISODate("2013-10-01T05:00:00Z"),
dateExpired: ISODate("2013-10-01T16:38:16.163Z")
}
$mul:
Multiply the value of a field by a number. To specify a $mul
expression, use the following prototype:
{ $mul: { <field1>: <number1>, ... } }
The field to update must contain a numeric value.
To specify a <field> in an embedded document or in an array,
use dot notation.
Behavior
Missing Field
If the field does not exist in a document, $mul creates the
field and sets the value to zero of the same numeric type as
the multiplier.
Atomic
$mul is an atomic operation within a single document.
Examples:
Multiply the Value of a Field
Consider a collection products with the following
document:
{ "_id" : 1, "item" : "ABC", "price" : NumberDecimal("10.99"),
"qty" : 25 }
The following db.collection.update() operation updates the
document, using the $mul operator to multiply the price by
1.25 and the qty field by 2:
db.products.update(
{ _id: 1 },
{ $mul: { price: NumberDecimal("1.25"), qty: 2 } } )
The operation results in the following document, where the
new value of price reflects the original value 10.99 multiplied
by 1.25 and the new value of qty reflects the original value of
25 multipled by 2:
{ "_id" : 1, "item" : "ABC", "price" : NumberDecimal("13.7375"),
"qty" : 50 }
Apply $mul Operator to a Non-existing Field:
Consider a collection products with the following document:
{ _id: 2, item: "Unknown" }
The following db.collection.update() operation updates the
document, applying the $mul operator to the field price that
does not exist in the document
db.products.update(
{ _id: 2 },
{ $mul: { price: NumberLong(100) } }
)
The operation results in the following document with a price
field set to value 0 of numeric type NumberLong, the same
type as the multiplier:
{ "_id" : 2, "item" : "Unknown", "price" : NumberLong(0) }
Multiply Mixed Numeric Types
Consider a collection products with the following document:
{ _id: 3, item: "XYZ", price: NumberLong(10) }
The following db.collection.update() operation uses the $mul
operator to multiply the value in the price field
NumberLong(10) by NumberInt(5):
db.products.update(
{ _id: 3 },
{ $mul: { price: NumberInt(5) } }
)
The operation results in the following document:
{ "_id" : 3, "item" : "XYZ", "price" : NumberLong(50) }
$rename:
The $rename operator updates the name of a field and has
the following form:
{$rename: { <field1>: <newName1>, <field2>: <newName2>,
... } }
The new field name must differ from the existing field name.
To specify a <field> in an embedded document, use dot
notation.
Consider the following example:
db.students.update( { _id: 1 }, { $rename: { 'nickname':
'alias', 'cell': 'mobile' } } )
This operation renames the field nickname to alias, and the
field cell to mobile.
Behavior
The $rename operator logically performs an $unset of
both the old name and the new name, and then
performs a $set operation with the new name.
As such, the operation may not preserve the order of
the fields in the document; i.e. the renamed field may
move within the document.
If the document already has a field with the
<newName>, the $rename operator removes that field
and renames the specified <field> to <newName>.
If the field to rename does not exist in a document,
$rename does nothing (i.e. no operation).
For fields in embedded documents, the $rename
operator can rename these fields as well as move the
fields in and out of embedded documents.
$rename does not work if these fields are in array
elements.
Examples
A collection students contains the following documents
where a field name appears misspelled, i.e. should be
name:
{
"_id": 1,
"alias": [ "The American Cincinnatus", "The American
Fabius" ],
"mobile": "555-555-5555",
"nmae": { "first" : "george", "last" : "washington" }
}
{
"_id": 2,
"alias": [ "My dearest friend" ],
"mobile": "222-222-2222",
"nmae": { "first" : "abigail", "last" : "adams" }
}
{
"_id": 3,
"alias": [ "Amazing grace" ],
"mobile": "111-111-1111",
"nmae": { "first" : "grace", "last" : "hopper" }
}
The examples in this section successively updates the
documents in the collection.
Rename a Field
To rename a field, call the $rename operator with the
current name of the field and the new name:
db.students.updateMany( {}, { $rename: { "nmae": "name" } }
)
This operation renames the field nmae to name for all
documents in the collection:
{
"_id": 1,
"alias": [ "The American Cincinnatus", "The American
Fabius" ],
"mobile": "555-555-5555",
"name": { "first" : "george", "last" : "washington" }
}{ "_id" : 2,
"alias" : [ "My dearest friend" ],
"mobile" : "222-222-2222",
"name" : { "first" : "abigail", "last" : "adams" } } }
{ "_id" : 3,
"alias" : [ "Amazing grace" ],
"mobile" : "111-111-1111",
"name" : { "first" : "grace", "last" : "hopper" } }
Rename a Field in an Embedded Document
To rename a field in an embedded document, call the
$rename operator using the dot notation to refer to the
field.
If the field is to remain in the same embedded document,
also use the dot notation in the new name, as in the
following:
db.students.update( { _id: 1 }, { $rename: { "name.first":
"name.fname" } } )
This operation renames the embedded field first to fname:
{
"_id" : 1,
"alias" : [ "The American Cincinnatus", "The American Fabius"
],
"mobile" : "555-555-5555",
"name" : { "fname" : "george", "last" : "washington" }
}
Rename a Field That Does Not Exist
When renaming a field and the existing field name refers to a
field that does not exist, the $rename operator does nothing,
as in the following:
db.students.update( { _id: 1 }, { $rename: { 'wife': 'spouse' } } )
This operation does nothing because there is no field named
wife.
$set:
The $set operator replaces the value of a field with the
specified value.
The $set operator expression has the following form:
{ $set: { <field1>: <value1>, ... } }
To specify a <field> in an embedded document or in an
array, use dot notation.
Behavior
If the field does not exist, $set will add a new field with
the specified value, provided that the new field does
not violate a type constraint.
If you specify a dotted path for a non-existent field, $set
will create the embedded documents as needed to
fulfill the dotted path to the field.
If you specify multiple field-value pairs, $set will update
or create each field.
Examples
Consider a collection products with the following document:
{
_id: 100,
sku: "abc123",
quantity: 250,
instock: true,
reorder: false,
details: { model: "14Q2", make: "xyz" },
tags: [ "apparel", "clothing" ],
ratings: [ { by: "ijk", rating: 4 } ]
}
Set Top-Level Fields
For the document matching the criteria _id equal to 100, the
following operation uses the $set operator to update the
value of the quantity field, details field, and the tags field.
db.products.update(
{ _id: 100 },
{ $set:
{
quantity: 500,
details: { model: "14Q3", make: "xyz" },
tags: [ "coats", "outerwear", "clothing" ]
}})
The operation replaces the value of: quantity to 500; the
details field to a new embedded document, and the tags
field to a new array.
Set Fields in Embedded Documents
To specify a <field> in an embedded document or in an
array, use dot notation.
For the document matching the criteria _id equal to 100,
the following operation updates the make field in the
details document:
db.products.update(
{ _id: 100 },
{ $set: { "details.make": "zzz" } }
)
Set Elements in Arrays
To specify a <field> in an embedded document or in an
array, use dot notation.
For the document matching the criteria _id equal to 100,
the following operation updates the value second element
(array index of 1) in the tags field and the rating field in the
first element (array index of 0) of the ratings array.
db.products.update(
{ _id: 100 },
{ $set:
{
"tags.1": "rain gear",
"ratings.0.rating": 2
}})
$setOnInsert:
If an update operation with upsert: true results in an insert
of a document, then $setOnInsert assigns the specified
values to the fields in the document.
If the update operation does not result in an insert,
$setOnInsert does nothing.
You can specify the upsert option for either the
db.collection.update() or db.collection.findAndModify()
methods.
db.collection.update(
<query>,
{ $setOnInsert: { <field1>: <value1>, ... } },
{ upsert: true }
)
To specify a <field> in an embedded document or in an
array, use dot notation.
Example:
A collection named products contains no documents.
Then, the following db.collection.update() with upsert: true
inserts a new document.
db.products.update(
{ _id: 1 },
{
$set: { item: "apple" },
$setOnInsert: { defaultQty: 100 }
},
{ upsert: true }
)
MongoDB creates a new document with _id equal to 1 from the
<query> condition, and then applies the $set and $setOnInsert
operations to this document.
The products collection contains the newly-inserted document:
{ "_id" : 1, "item" : "apple", "defaultQty" : 100 }
Field Update
Summary: Operator
Array
Update
Operator
Thank You………
If you have any quries please write to info@uplatz.com".