Scorocode.UpdateOps
new UpdateOps()
Class for multiple object update operations
Example
var Scorocode = require('scorocode');
Scorocode.Init({
ApplicationID: "applicationId",
JavaScriptKey: "javascriptKey"
});
var Items = new Scorocode.Query("items");
var updateItems = new Scorocode.UpdateOps("items");
Items.notEqualTo("price", 42)
.find()
.then((result) => {
updateItems.set("price", 42);
return Items.update(updateItems)
})
.then((updated) => {
console.log(updated);
})
.catch((error) => {
console.log("Something went wrong: \n", error)
});
Returns: UpdateOps
- Returns the Scorocode.UpdateOps instance
.set(data)
Method for transferring data to object
Parameter | Type | Description |
---|---|---|
data | Object |
Data in {"key", "value"} format, where key is the collection field name. |
Example
var Scorocode = require('scorocode');
Scorocode.Init({
ApplicationID: "applicationId",
JavaScriptKey: "javascriptKey"
});
var Items = new Scorocode.Query("items");
var updateItems = new Scorocode.UpdateOps("items");
Items.notEqualTo("price", 42)
.find()
.then((result) => {
updateItems.set("price", 42);
return Items.update(updateItems)
})
.then((updated) => {
console.log(updated);
})
.catch((error) => {
console.log(error)
});
See
.push(key, value)
Method for adding an element to an array
Parameter | Type | Description |
---|---|---|
key | String |
Name of the field whose value should be updated |
value | String / Number / Boolean / Date / Array / Object |
Value of the new array element |
Example
var Scorocode = require('scorocode');
Scorocode.Init({
ApplicationID: "applicationId",
JavaScriptKey: "javascriptKey"
});
var Items = new Scorocode.Query("items");
var updateItems = new Scorocode.UpdateOps("items");
Items.exists("arrayField")
.find()
.then((result) => {
updateItems.push("arrayField", "New element");
return Items.update(updateItems)
})
.then((updated) => {
console.log(updated);
})
.catch((error) => {
console.log(error)
});
.pull(key, value)
Method for removing all array elements whose values are the same as the specified one.
Parameter | Type | Description |
---|---|---|
key | String |
Name of the field whose value should be updated |
value | String / Number / Boolean / Date / Array / Object |
Value to be removed |
Example
var Scorocode = require('scorocode');
Scorocode.Init({
ApplicationID: "applicationId",
JavaScriptKey: "javascriptKey"
});
var Items = new Scorocode.Query("items");
var updateItems = new Scorocode.UpdateOps("items");
Items.exists("arrayField")
.find()
.then((result) => {
updateItems.pull("arrayField", {"remove": true});
return Items.update(updateItems)
})
.then((updated) => {
console.log(updated);
})
.catch((error) => {
console.log("Something went wrong \n", error)
});
Exceptions:
Error
"For a new document use the method Set"Error
'Field must by a type of array'
.pullAll(key, value)
Method for removing all array elements whose values are the same as one of the specified values.
Parameter | Type | Description |
---|---|---|
key | String |
Name of the field whose value should be updated |
value | Array |
Array of values to be removed |
Example
var Scorocode = require('scorocode');
Scorocode.Init({
ApplicationID: "applicationId",
JavaScriptKey: "javascriptKey"
});
var Items = new Scorocode.Query("items");
var updateItems = new Scorocode.UpdateOps("items");
Items.exists("arrayField")
.find()
.then((result) => {
updateItems.pullAll("arrayField", ["Delete me", 42, {"Important": false}]);
return Items.update(updateItems)
})
.then((updated) => {
console.log(updated);
})
.catch((error) => {
console.log(error)
});
Exceptions:
Error
'For a new document use the method Set'Error
'Field must by a type of array'Error
'Value must by a type of array'
.addToSet(key, value)
Method for adding an element to an array only if there are no elements with the same name in the array.
Parameter | Type | Description |
---|---|---|
key | String |
Name of the field whose value should be updated |
value | String / Number / Boolean / Date / Array / Object |
Value of the added element |
Example
var Scorocode = require('scorocode');
Scorocode.Init({
ApplicationID: "applicationId",
JavaScriptKey: "javascriptKey"
});
var Items = new Scorocode.Query("items");
var updateItems = new Scorocode.UpdateOps("items");
Items.exists("arrayField")
.find()
.then((result) => {
updateItems.addToSet("arrayField", ["First element of new element", {"second": true}]);
return Items.update(updateItems)
})
.then((updated) => {
console.log(updated);
})
.catch((error) => {
console.log(error)
});
Exceptions:
Error
'For a new document use the method Set'Error
'Field must by a type of array'
.pop(key, pos)
Method for removing the first or the last array element
Parameter | Type | Description |
---|---|---|
key | String |
Name of the field whose value should be updated |
pos | Number |
Position of the element to be removed in the array: -1 for the first element and 1 for the last |
Example
var Scorocode = require('scorocode');
Scorocode.Init({
ApplicationID: "applicationId",
JavaScriptKey: "javascriptKey"
});
var Items = new Scorocode.Query("items");
var updateItems = new Scorocode.UpdateOps("items");
Items.exists("arrayField")
.find()
.then((result) => {
updateItems.pop("arrayField", 1);
return Items.update(updateItems)
})
.then((updated) => {
console.log(updated);
})
.catch((error) => {
console.log(error)
});
Exceptions:
Error
'For a new document use the method Set'Error
'Field must by a type of array'
.inc(key, amount)
The method increments the numeric field value by a defined number
Parameter | Type | Description |
---|---|---|
key | String |
Name of the field whose value should be updated |
amount | Number |
Increment step |
Example
var Scorocode = require('scorocode');
Scorocode.Init({
ApplicationID: "applicationId",
JavaScriptKey: "javascriptKey"
});
var Items = new Scorocode.Query("items");
var updateItems = new Scorocode.UpdateOps("items");
Items.exists("price")
.find()
.then((result) => {
updateItems.inc("price", -2.42);
return Items.update(updateItems)
})
.then((updated) => {
console.log(updated);
})
.catch((error) => {
console.log(error)
});
Exceptions:
Error
'For a new document use the method Set'Error
'Field must by a type of number'
.currentDate()
Sets the current time as the field's value
Parameter | Type | Description |
---|---|---|
key | String |
Name of the field whose value should be updated |
type | String / Boolean |
Date type. Accepts the following values: true, "date" or "timestamp" |
Example:
var Scorocode = require('scorocode');
Scorocode.Init({
ApplicationID: "applicationId",
JavaScriptKey: "javascriptKey"
});
var Items = new Scorocode.Query("items");
var updateItems = new Scorocode.UpdateOps("items");
Items.find()
.then((result) => {
updateItems.currentDate("someDate", "timestamp");
return Items.update(updateItems)
})
.then((updated) => {
console.log(updated);
})
.catch((error) => {
console.log(error)
});
Exceptions:
Error
'For a new document use the method Set'Error
'Invalid type'
.mul(key, number)
The method multiplies the numeric field value by a defined number
Parameter | Type | Description |
---|---|---|
key | String |
Name of the field whose value should be updated |
number | Number |
Multiplier |
Example
var Scorocode = require('scorocode');
Scorocode.Init({
ApplicationID: "applicationId",
JavaScriptKey: "javascriptKey"
});
var Items = new Scorocode.Query("items");
var updateItems = new Scorocode.UpdateOps("items");
Items.exists("price")
.find()
.then((result) => {
updateItems.mul("price", 0.5);
return Items.update(updateItems)
})
.then((updated) => {
console.log(updated);
})
.catch((error) => {
console.log(error)
});
Exceptions:
Error
'For a new document use the method Set'Error
'Field must by a type of number'Error
'Value must by a type of number'
.min()
The method updates the numeric field value only if the new value is less than the current field value
Parameter | Type | Description |
---|---|---|
key | String |
Name of the field whose value should be updated |
number | Number |
New value |
Example
var Scorocode = require('scorocode');
Scorocode.Init({
ApplicationID: "applicationId",
JavaScriptKey: "javascriptKey"
});
var Items = new Scorocode.Query("items");
var updateItems = new Scorocode.UpdateOps("items");
Items.exists("price")
.find()
.then((result) => {
updateItems.min("price", 42);
return Items.update(updateItems)
})
.then((updated) => {
console.log(updated);
})
.catch((error) => {
console.log(error)
});
Exceptions:
Error
'For a new document use the method Set'Error
'Field must by a type of number'Error
'Value must by a type of number'
.max()
The method updates the numeric field value only if the new value is greater than the current field value
Parameter | Type | Description |
---|---|---|
key | String |
Name of the field whose value should be updated |
number | Number |
New value |
Example
var Scorocode = require('scorocode');
Scorocode.Init({
ApplicationID: "applicationId",
JavaScriptKey: "javascriptKey"
});
var Items = new Scorocode.Query("items");
var updateItems = new Scorocode.UpdateOps("items");
Items.exists("price")
.find()
.then((result) => {
updateItems.max("price", 42);
return Items.update(updateItems)
})
.then((updated) => {
console.log(updated);
})
.catch((error) => {
console.log(error)
});
Exceptions:
Error
'For a new document use the method Set'Error
'Field must by a type of number'Error
'Value must by a type of number'