update(): Modify a document in a collection
Modifies a document in a collection by using z/TPF support for MongoDB.
- Replace an existing document completely or insert a new document if the document does not exist.
- Index or deindex an existing document.
- Change the location of a document.
- Create or remove logical records (LRECs).
- Change the value of a field in an existing document.
Last updated
- Changed in 2020.
- Changed for PUT14.
- Added for PUT13.
Format
db.collection.update(query,update,options)
- collection
- The name of the collection that the document is in. The name of the collection is the name that is defined in the z/TPFDF collection descriptor.
- query
- A JSON document that defines which document
to update. The JSON document must be in the
following
format:
Specify one or more of the following fields:_interleave: interleaveNum, /*Optional*/ _partition: partitionNum, /*Optional*/ _tenant: "tenantName", /*Optional*/ _index.IndexName: IndexNameSpecification, _id: ObjectId (objectIdValue), conditionalField: conditionalValue /*Optional*/- _interleave: interleaveNum
- Use this field to access data for a z/TPFDF
interleave, where interleaveNum is an integer number that represents the target
interleave.
If this field is not specified for a collection or z/TPFDF file that is defined as interleaved, an interleave number of 0 is assumed. If the z/TPFDF file is not defined as interleaved, specifying this field has no effect.
This field is optional.
- _partition: partitionNum
- Use this field to access data for a z/TPFDF
partition, where partitionNum is an integer number that represents the target
partition.
If this field is not specified for a collection or z/TPFDF file that is defined as partitioned, a partition number of 0 is assumed. If the z/TPFDF file is not defined as partitioned, specifying this field has no effect.
This field is optional.
- _tenant: tenantName
- Use this field to access data for a subsystem user, where tenantName is the
name that represents the target subsystem user.
If this field is not specified for a collection or z/TPFDF file that is subsystem unique, the default subsystem user for the subsystem is assumed. If the z/TPFDF file is defined as subsystem user shared, specifying this field has no effect.
This field is optional.
- _index.IndexName: IndexNameSpecification
- Use this field to locate a document based on the index that is defined in the z/TPFDF collection descriptor, where IndexName is the name of
an index element that is defined in the z/TPFDF collection descriptor,
and IndexNameSpecification is the document that specifies the fields that are
defined for the index element in the z/TPFDF collection descriptor and
the value of the fields.
You must specify the fields of the index name in the order that these fields are defined in the DFDL schema file. If you do not specify the value for a field of an index name, the default value that is defined in the DFDL schema file for the z/TPFDF file is used.
The _index.IndexName field uses dot notation because IndexName is an embedded document of _index. When you specify fields with dot notation in the MongoDB shell, you must enclose the field name in single or double quotation marks.
This field is required unless the _id field is specified. If you specify both fields, the _id field is used to locate the document, and the _index.IndexName field is ignored.
- _id: format
- Use this field to access a document directly by using the _id field, where
format can be one of the following values:
- For unfiltered collections, the ObjectId value that represents the z/TPF file address that is used to access the document. Specify the field as _id: ObjectId(objectIdValue), where objectIdValue is the ObjectId value.
- For filtered collections, a document that represents the filtered data that is used to access the document that you want to retrieve. The document that represents the filtered data includes the ObjectId value, index name, and algorithm string. For more information, see Identify a document in a filtered collection.
This field is required unless the _index.IndexName field is specified. If you specify both fields, the _id field is used to locate the document, and the _index.IndexName field is ignored.
- conditionalField: conditionalValue
- Use this field with the _index or _id field to
conditionally return the document based on the contents of the document, where
conditionalField is a field in the document that is defined in the DFDL schema file that describes this document, and
conditionalValue is the value of the field. Specify this field in one of the
following formats:
- LRECs in a document are represented as an array, so you must use the
$elemMatch operator to conditionally find documents based on fields in an LREC.
lrecName: { $elemMatch: {conditionalField: conditionalValue, ⋮ conditionalField: conditionalValue } }- lrecName
- The name of the record that is defined in the z/TPFDF collection descriptor and DFDL schema file.
- $elemMatch
- Use this operator to conditionally find documents based on fields in an LREC.
You can specify multiple lrecName conditions, and for each lrecName, you can specify multiple fields in the LREC.
- For z/TPFDF files with
SEQUPDT=YES specified in the z/TPFDF database definition, you can use the
_seq field as a conditional field with the value of the current file sequence
update counter of the document:
_seq: sequenceValue- _seq
- The file sequence update counter that z/TPFDF
maintains.
When the z/TPFDF database definitions for the z/TPFDF files have SEQUPDT=YES specified, all documents that are returned by using z/TPF support for MongoDB for the collection that represents the z/TPFDF file contain an _seq field with the current file sequence update counter of the document as the value.
When you update the document, use the _seq field as a conditional field to ensure that the document is not changed in between the time the document is queried and updated.
- sequenceValue
- The current file sequence update counter of the document that is obtained from a previous query.
The document is updated if all of the conditional field specifications match. Otherwise, a not found condition occurs. To determine whether the document does not exist or the document exists but the conditional fields do not match, issue a subsequent MongoDB findOne() operation without the conditional fields specified.
This field is optional.
- LRECs in a document are represented as an array, so you must use the
$elemMatch operator to conditionally find documents based on fields in an LREC.
- update
- A JSON document that specifies the updates to
the document that is located by using the query field. Specify the document in
one of the following formats:
- To replace the located document, specify the LRECs and fields that make up the document in the
z/TPF database. Each lrecName
is represented as an array of that type of record. Specify 0 or more lrecName
fields in the following format:
lrecName: [{lrecSpecification}, ⋮ {lrecSpecification}], ⋮ lrecName: [{lrecSpecification}, ⋮ {lrecSpecification}]- lrecName
- The name of the record that is defined in the z/TPFDF collection descriptor and DFDL schema file.
- lrecSpecification
- The fields of the record that is defined in the DFDL schema file and the value of each field. You must specify
the fields of the record in the order that these fields are defined in the DFDL schema file. Specify the fields of the LREC and their
values in the following format:
"Field1" : value, "Field2" : value, ⋮ "Fieldn" : value,- n
- The number of fields that you specify.
- To insert or remove an index or indexes, specify the document in one of the following formats:
- Specify an index name and the value of the index to be added or
removed:
{operator: {_index.indexName: {IndexNameSpecification}}} - Specify an index name with the $each operator to index or deindex for each
indexNameSpecification:
{operator: {_index.indexName: {$each [{IndexNameSpecification}, ⋮ {IndexNameSpecification}] }
- operator
- You can specify one of the following operators:
- $push
- Index the document for the specified index names.
- $pull
- Deindex the document for the specified index names.
- indexName
- The name of an index element that is defined in the z/TPFDF collection descriptor.
- IndexNameSpecification
- The document that specifies the fields that are defined for the index element in the z/TPFDF collection descriptor and the value of the fields.
The _index.IndexName field uses dot notation because IndexName is an embedded document of the _index field. When you specify fields with dot notation in the MongoDB shell, you must enclose the field name in single or double quotation marks.
You can specify more than one index name as part of a update() operation to be added or removed from the located document. To specify more than one index name, use the following format:_index.IndexName: {IndexSpecification}, ⋮ _index.IndexName: {IndexSpecification} - Specify an index name and the value of the index to be added or
removed:
- To change the location of a document, specify the document
in the following format:
{ $set: { _location: locationName } }- $set
- Changes the value of a field.
- _location
- Specifies the location that the document is to be stored in after the document is successfully
updated. You must specify this field to move a document from its current location.
If you do not specify this field, the document is not moved, regardless of whether the document is on the local z/TPF system or a remote data store.
- locationName
- The location name. You can specify one of the following values:
- The name of the target remote data store that is defined in a deployed remote data store descriptor. The document will be moved to the specified remote data store by using z/TPFDF remote subfile support.
- A NULL value. The document is set to be on the local z/TPF system. If the document is stored remotely, z/TPF system deletes the document from the remote data store and stores the subfile locally on the z/TPF system.
- To insert or remove an LREC, specify the document in one of the following formats:
- Specify an LREC and the value of the LREC to be added or
removed:
{operator: {lrecName: {lrecSpecification}}} - Specify an LREC with the $each operator to insert or remove multiple values
for an LREC:
{operator: {lrecName: {$each [{lrecSpecification}, ⋮ {lrecSpecification}] }- operator
- You can specify one of the following operators:
- $push
- Add the specified LREC.
- $pull
- Remove the specified LREC.
- lrecName
- The name of an LREC that is defined in the z/TPFDF collection descriptor and DFDL schema file.
- lrecSpecification
- The document that specifies the fields of an LREC that are defined in the DFDL schema file and the value of the fields. You must specify
the fields of the LREC in the order that these fields are defined in the DFDL schema file. Specify the fields of the LREC and their
values in the following format:
"Field1" : value, "Field2" : value, ⋮ "Fieldn" : value- n
- The number of fields that you specify.
- Specify an LREC and the value of the LREC to be added or
removed:
- To change the value of a field in a document, specify the document with the
$set operator in one of the following formats:
- Change the value of one field in a
document:
{$set: {fieldName: {value}}} - Change the values of more than one field in a
document:
{$set: {fieldName: {value}, ⋮ fieldName: {value} } }
- $set
- Changes the value of a field.
- fieldName
- The fully qualified name of the field in the document that you want to update.
You can specify the name of a key field for a collection if the default keys for the collection are defined in the z/TPFDF collection descriptor.
Specify fieldName in the following format in the dot notation form. When you specify fields with dot notation in the MongoDB shell, you must enclose the field name in single or double quotation marks.
'lrecname.lrecnumber.field'- lrecname
- The name of the record that is defined in the z/TPFDF collection descriptor and DFDL schema file.
- lrecnumber
- The array index of the record that is being updated. If you specify 0, the first record in the
document with the record name that you specified is updated. If the specified array index is not
found for this record name, an error occurs.
If you specify the $ positional operator, the record in the document that is specified by using the query parameter is updated. You must specify the record name in an $elemMatch statement of the document, or an error occurs.
- field
- The field that you want to update.
- value
- The value that you want to change to.
- Change the value of one field in a
document:
- To replace the located document, specify the LRECs and fields that make up the document in the
z/TPF database. Each lrecName
is represented as an array of that type of record. Specify 0 or more lrecName
fields in the following format:
- options
- The options that you can specify on the update() method.
- upsert
- Use this option to insert a document into the collection if the document does not already exist.
If no matching document for a replace request exists and you specify the upsert option as true, the z/TPF system inserts the document that is specified on the update parameter into the collection.
If a matching document exists, the upsert option is ignored and the z/TPF system updates the existing documents based on the document that is specified on the update parameter.
The default value of the upsert option is false.
Return conditions
If an error occurs, the errmsg field of the document that is returned contains the information about why the document could not be updated.
Programming considerations
- The indexes in the z/TPFDF database are predefined, so you cannot add new indexes by using the createIndex() method or remove indexes by using the dropIndex() method.
- z/TPF support for MongoDB does not support the multi option on the update() method. The default value of false is assumed if you do not specify this option. If you specify the multi option with the value of true, the request is rejected.
- The z/TPF system assigns the _id value for the document as the z/TPF file address. If you specify the_id field in the document that is specified by the update parameter, the value in the _id field is ignored.
- The z/TPF system manages the _seq value for the document. If you specify the _seq field in the document that is specified for the update parameter, the value of the _seq field is ignored.
- Specifying the _interleave, _partition, or _tenant field applies the value to all index names that are specified on the update() request. To insert or remove indexes in multiple partitions, interleaves, or tenants, you must enter multiple update() requests.
- If you define automatic indexing rules for index names in the z/TPFDF collection descriptor, you do not need to index or deindex documents for these index names manually.
- The z/TPF system does not use the content of the document that is specified on the query parameter to construct the document that is inserted into the collection because of the upsert option.
- When a document is being inserted because of the upsert option, the z/TPF system returns an error if both of the following situations occur:
- The _index keyword does not exist.
- Automatic indexing cannot be applied to the document that is inserted.
- If the default keys for a collection are not defined in the z/TPFDF collection descriptor, this method cannot be used to update the key fields by using the $set operator.
- When you update the value of a key field by using the $set operator, the position of the updated logical record might be changed, and subsequent updates to the logical record are not allowed. For example, a request to update fieldA, fieldB, and key1 of a logical record can be processed. However, a request to update key1, fieldA, and fieldB of a logical record is rejected, because the position of the logical record might be changed after the key1 field is updated.
- When you insert or update LRECs in filtered collections, the inserted or updated LRECs must conform to the filter rule that is defined in the z/TPFDF collection descriptor. Otherwise, the request to insert or update the LRECs is rejected.
Examples
db.PNR.update
(
{
_id: ObjectId("00000000000010000ef55788")
},
{
"PassengerNumberRecord":
[
{
"PassengerNumber": NumberLong(587690)
}
],
"PassengerNameRecord":
[
{
"PassengerName": "AImee Xonti "
}
],
"AddressRecord":
[
{
"Address": "905 Innis Lane, Bethesda, Maryland 47304"
}
],
"FlightHistoryRecord":
[
{
"FlightInfo":
{
"Flight":
{
"FlightDate": 1,
"FlightNumber": "10 "
},
"Origin": "FXH",
"Destination": "BKF"
}
},
{
"FlightInfo":
{
"Flight":
{
"FlightDate": 65,
"FlightNumber": "586"
},
"Origin": "GAT",
"Destination": "SNB"
}
}
]
}
);//locate a document in the PNR collection by using the PnrByNumber index with the value of 1
DB db= (new MongoClient("ztpf.mydomain.com",27017)).getDB("tpfdf");
dBCollection = db.getCollection("PNR");
String queryPnr="{'_index.PnrByNumber':{number:1}}";
DBObject queryDoc=(DBObject) JSON.parse(queryPnr);
DBObject modDoc= dBCollection.findOne(queryDoc);
//insert a new PassengerNameRecord into the retrieved PNR document
//The variable temp is referencing the storage within the retrieved document (modDoc).
BasicDBList temp= (BasicDBList) modDoc.get("PassengerNameRecord");
BasicDBObject newStuff= new BasicDBObject("PassengerName","EXTRA NAME" );
temp.put(temp.size(),newStuff );
//replace the contents of the PNR document
dBCollection.update(queryDoc, modDoc);
db.PNR.update
(
{
'_index.PnrByName':{"name":"ABEDFORD"}
},
{
$push:
{
'_index.PnrByNumber': {number: 1},
'_index.PnrByName': {name:"AASMITH"}
}
}
);db.PNR.update
(
{
'_index.PnrByName':{"name":"ZZSMITH"}
},
{
$push:
{
'_index.PnrByNumber': {$each: [ {number:1}, {number:2}]},
'_index.PnrByName':{name:"AASMITH"}
}
}
);
db.PNR.update
(
{
'_index.PnrByName':{"name":"ZZSMITH"}
},
{
$push:
{
'_index.PnrByNumber': {number: 2}
},
$pull:
{
'_index.PnrByNumber': {number: 1}
}
}
);import com.mongodb.*;
MongoClient mongoClient = new MongoClient( "ztpf.mydomain.com");
DB db = mongoClient.getDB( "tpfdf" );
DBCollection coll = db.getCollection("PNR");
DBObject query= (DBObject)JSON.parse("{'_index.PnrByName':{'name':'ZZSMITH'}}");
DBObject update = ( DBObject ) JSON.parse(" {'$push':{ '_index.PnrByNumber': {number: 2}},
'$pull': {'_index.PnrByNumber': {number: 1}}}");
dBCollection.update(query,update); db.PNR.update
(
{
_id: ObjectId("00000000000010000ef55788"),
_seq: 12345
},
{
$push:
{
'_index.PnrByNumber': {$each: [ {number:1}, {number:2}]},
'_index.PnrByName':{name:"AASMITH"}
}
}
);db.PNR.update
(
{
_index.PnrByName:{"name":"ABEDFORD"}
},
{
$push:
{
PassengerNumberRecord:{PassengerNumber:1000}
}
}
);import com.mongodb,*;
MongoClient mongoClient = new MongoClient("ztpf.mydomain.com");
DB db = mongoClient.getDB( "tpfdf" );
DBCollection coll = db.getCollection("PNR");
DBObject query= (DBObject)JSON.parse("{'_index.PnrByName':{'name': 'ABEDFORD' }}");
DBObject update=( DBObject ) JSON.parse
("{$push:{PassengerNumberRecord:{PassengerNumber:1000}}}");
dBCollection.update(query,update);db.PNR.update
(
{
_index.PnrByName:{"name":"ABEDFORD"}
},
{
$push:
{
PassengerNumberRecord:{$each:[{PassengerNumber:1000}, {PassengerNumber:1002}]}
}
}
)
db.PNR.update
(
{
_index.PnrByName:{"name":"ABEDFORD"}
},
{
$push:
{
"_index.PnrByNumber":{number:1000},PassengerNumberRecord:{PassengerNumber:1000},
"_index.PnrByName":{name:"MBEDFORD"},PassengerNameRecord:{PassengerName:"MBEDFORD"}
}
}
)db.PNR.update
(
{
_index.PnrByName:{"name":"ABEDFORD"}
},
{
$pull:
{
PassengerNumberRecord:{PassengerNumber:1000}
}
}
);db.PNR.update
(
{
_index.PnrByName:{"name":"ABEDFORD"}
},
{
$pull:
{
PassengerNumberRecord:{$each:[{PassengerNumber:1000}, {PassengerNumber:1002}]}
}
}
)
db.PNR.update
(
{
_index.PnrByName:{"name":"ABEDFORD"}
},
{
$pull:
{
"_index.PnrByNumber":{number:1000},PassengerNumberRecord:{PassengerNumber:1000},
"_index.PnrByName":{name:"MBEDFORD"},PassengerNameRecord:{PassengerName:"MBEDFORD"}
}
}
)db.PNR.update
(
{
'_index.PnrByName':{"name":"Joe Reagan"}
},
{
$set:{'FlightHistoryRecord.0.FlightInfo.Flight.FlightNumber': '100'}
}
)import com.mongodb.*;
MongoClient mongoClient = new MongoClient("ztpf.mydomain.com");
DB db = mongoClient.getDB( "tpfdf" );
DBCollection coll = db.getCollection("PNR");
DBObject query= (DBObject)JSON.parse("{'_index.PnrByName':{'name': 'ZZSMITH' }}");
DBObject update = ( DBObject ) JSON.parse
(" {$set:{'FlightHistoryRecord.0.FlightInfo.Flight.FlightNumber':'100'}}");
dBCollection.update(query,update);db.PNR.update
(
{
'_index.PnrByName': {"name":"Joe Reagan"},
"FlightHistoryRecord": {
"$elemMatch": {'FlightInfo.Flight.FlightDate': 1}
}
},
{
$set:{'FlightHistoryRecord.$.FlightInfo.Flight.FlightNumber': "100"}
}
)db.PNR.update
(
{
'_index.PnrByName': {"name":"Joe Reagan"},
"FlightHistoryRecord": {
"$elemMatch": {'FlightInfo.Flight.FlightDate': 1}
}
},
{
$set:{'FlightHistoryRecord.$': {
"FlightInfo": {
"Flight": {
"FlightDate": 1,
"FlightNumber": "10 "
},
"Origin": "HPN",
"Destination": "SAV"
}
}
}
}
)db.PNR.update(
{
_index.PnrByName:{name:"ZZSMITH"}
},
{
"PassengerNameRecord" : [
{
"PassengerName": "ZZSMITH"
}
],
"PassengerNumberRecord" : [
{
"PassengerNumber" : NumberLong(99999)
}
],
"AddressRecord" : [
{
"Address" : "267 Jordan Blvd, LasVegas, Nevada 58521"
}
],
"FlightHistoryRecord" : [
{
"Spare" : "\u0000",
"FlightInfo" : {
"Flight" : {
"FlightDate" : 318,
"FlightNumber" : "157"
},
"Origin" : "GLV",
"Destination" : "ETD"
}
}
]
},
{
upsert:true
})
db.PNR.update(
{
_index.PnrByName:{name:"ZZSMITH"}
},
{
_index:{"PnrByNumber":{number:44444}},
"PassengerNameRecord" : [
{
"PassengerName": "ZZSMITH"
}
],
"PassengerNumberRecord" : [
{
"PassengerNumber" : NumberLong(99999)
}
],
"AddressRecord" : [
{
"Address" : "267 Jordan Blvd, LasVegas, Nevada 58521"
}
],
"FlightHistoryRecord" : [
{
"Spare" : "\u0000",
"FlightInfo" : {
"Flight" : {
"FlightDate" : 318,
"FlightNumber" : "157"
},
"Origin" : "GLV",
"Destination" : "ETD"
}
}
]
},
{
upsert:true
})
db.PNR.update(
{ _index.PnrByName : { name : "ZZSMITH" } },
{ $set : { _location : “FAR_AWAY” } }
);
db.PNR.update(
{ _index.PnrByName : { name : "ZZSMITH" } },
{ $set : { _location : NULL } }
);
db.PNR.update(
{ _index.PnrByName : { name : "ZZSMITH" } },
{
“_location” : “FAR_AWAY”,
"PassengerNumberRecord":
[
{ "PassengerNumber": NumberLong(587690) }
],
"PassengerNameRecord": [
{ "PassengerName": “ZZSMITH" }
],
"AddressRecord":
[
{ "Address": "905 Innis Lane, Bethesda, Maryland 47304" }
],
"FlightHistoryRecord": [
{
"FlightInfo":
{
"Flight":
{
"FlightDate": 1,
"FlightNumber": "10"
},
"Origin": "FXH",
"Destination": "BKF"
}
}
]
}
);