How to update a document in Documentdb using queries? - azure-cosmosdb

How to update a document in Document db using queries ( basically want to update a document using a stored procedure)?

The following sample might be what you need: https://github.com/aliuy/documentdb-serverside-js/blob/master/stored-procedures/update.js.
Here's a simplified version:
function updateSproc(id, update) {
var collection = getContext().getCollection();
var collectionLink = collection.getSelfLink();
var response = getContext().getResponse();
tryQueryAndUpdate();
function tryQueryAndUpdate(continuation) {
var query = {query: "select * from root r where r.id = #id", parameters: [{name: "#id", value: id}]};
var requestOptions = {continuation: continuation};
var isAccepted = collection.queryDocuments(collectionLink, query, requestOptions, function (err, documents, responseOptions) {
if (err) throw err;
if (documents.length > 0) {
tryUpdate(documents[0]);
} else {
throw new Error("Document not found.");
}
});
}
function tryUpdate(document) {
var requestOptions = {etag: document._etag};
var fields, i;
fields = Object.keys(update);
for (i = 0; i < fields.length; i++) {
document[fields[i]] = update[fields[i]];
}
var isAccepted = collection.replaceDocument(document._self, document, requestOptions, function (err, updatedDocument, responseOptions) {
if (err) throw err;
response.setBody(updatedDocument);
});
}

Related

no records found in stored procedure cosmos db

I have written a simple cosmos stored procedure
function a()
{
var context = getContext();
var response = context.getResponse();
var collection = context.getCollection();
var collectionLink = collection.getSelfLink();
var filterQuery = 'SELECT * FROM c';
collection.queryDocuments(collectionLink, filterQuery, { pageSize: -1 },
function (err, documents) {
if (err) throw err;
if (!documents || !documents.length) {
var response = getContext().getResponse();
response.setBody('No Records found');
}
else {
response.setBody(response.getBody());
}
});
}
However on execution, I always get "No records found", though on running the query separately, I do get records. Also I have give partition key value.
Suppose you provide the incorrect partition key so that no data could be found.
SP codeļ¼š
function a()
{
var context = getContext();
var collection = context.getCollection();
var collectionLink = collection.getSelfLink();
var filterQuery = 'SELECT * FROM c';
collection.queryDocuments(collectionLink, filterQuery, { pageSize: -1 },
function (err, documents) {
if (err) throw err;
if (!documents || !documents.length) {
var response = getContext().getResponse();
response.setBody('No Records found');
}else {
var response = getContext().getResponse();
response.setBody(documents);
}
});
}
Sample data:
Provide partition key and execute:

Firestore transaction update multiple document

I want to update documents in one collection.
my_collection
document_1
field_1
field_2
document_2
field_1
field_2
My code:
exports.aggregateUsers =
functions.firestore.document('users/{userId}').onWrite(function(change,
context) {
const document = change.after.exists ? change.after.data() : null;
const oldDocument = change.before.data() || null;
return firestore.runTransaction(function(transaction) {
var oldInstanceRef;
var instanceRef;
var oldInstanceDoc;
var instanceDoc;
if (document != null) {
instanceRef = firestore.collection("counters").doc("instance_counter").collection("instances").doc(document.instance);
instanceDoc = transaction.get(instanceRef);
var newNumberOfUsers = (instanceDoc.data().number_of_users || 0) + 1;
transaction.set(instanceRef, { number_of_users: newNumberOfUsers });
}
if (oldDocument != null) {
oldInstanceRef = firestore.collection("counters").doc("instance_counter").collection("instances").doc(oldDocument.instance);
oldInstanceDoc = transaction.get(oldInstanceRef);
var newPrevNumberOfUsers = (oldInstanceDoc.data().number_of_users || 0) + 1;
transaction.set(instanceRef, { number_of_users: newPrevNumberOfUsers });
}
}).catch(function(error) {
console.log("invalid-argument", error.code, error.message);
});
});
Errors:
instanceDoc.data is not a function
I am using transcation in cloud function to aggregate number of users, not using distributed counter, because of low traffic app. My question is how to update field's value in each document? Thank you
I guess you forgot the async/await.
Try this:
async before transaction parameter
await before transaction.get(<some_ref_here>)
return firestore.runTransaction(async transaction => {
var oldInstanceRef;
var instanceRef;
var oldInstanceDoc;
var instanceDoc;
if (document != null) {
instanceRef = firestore.collection("counters").doc("instance_counter").collection("instances").doc(document.instance);
instanceDoc = await transaction.get(instanceRef);
var newNumberOfUsers = (instanceDoc.data().number_of_users || 0) + 1;
transaction.set(instanceRef, { number_of_users: newNumberOfUsers });
}
if (oldDocument != null) {
oldInstanceRef = firestore.collection("counters").doc("instance_counter").collection("instances").doc(oldDocument.instance);
oldInstanceDoc = await transaction.get(oldInstanceRef);
var newPrevNumberOfUsers = (oldInstanceDoc.data().number_of_users || 0) + 1;
transaction.set(instanceRef, { number_of_users: newPrevNumberOfUsers });
}
}).catch(function(error) {
console.log("invalid-argument", error.code, error.message);
});

Handling multiple ajax call using $q.all

I'm trying to call multiple ajax in my page using $q. after all the response am storing in one array. but it seems not working correctly-
My controller-
used for loop to go over multiple pages in API and get the json.
$scope.items = [];
for (var i = 1; i < 10; i++) {
var apiURL = "https://swapi.co/api/planets?page =" + i;
searchData(apiURL).then(function(response) {
$scope.items.push(response[0].data.results);
});
}
$scope.showDetail = function(data) {
$scope.items = data.results;
$scope.items.sort(function(a, b) {
return a.population.localeCompare(b.population);
});
}
$scope.showDetail($scope.items);
$scope.highlighFont = function() {
}
My Factory-
var app = angular.module('starApp');
app.factory('searchData', function($q, $http) {
return function(apiUrl) {
var promises = [];
var deffered = $q.defer();
$http({
method : 'GET',
url : apiUrl
}).then(function(data) {
deffered.resolve(data);
}, function(error) {
deffered.reject();
})
promises.push(deffered.promise);
return $q.all(promises);
}
});
can someone correct me if am doing wrong??
You need to call $q.all() in the controller
app.factory('searchData', function($q, $http) {
return function(apiUrl) {
return $http({
method : 'GET',
url : apiUrl
});//$http returns a promise
}
});
controller:
$scope.promises = [];
for (var i = 1; i < 10; i++) {
var apiURL = "https://swapi.co/api/planets?page =" + i;
$scope.promises.push(searchData(apiURL));
}
$q.all($scope.promises).then(function(results){
console.log(results);
});

NIghtmare error in web scraping course details

Hi i am using nightmare for scrape data from website and also course details. I occur a issue :-
err: { message: 'navigation error',
code: 0,
details: 'OK',
url: 'https://www.myskills.gov.au/courses/details?Code=CHC14015' }
on each url traversal. Please suggest me to resolve this:
var Nightmare = require('nightmare')
var vo = require('vo')
var fs = require('fs')
var filesystem = require('file-system')
// var nightmare = Nightmare({show:true});
var sleep = require('sleep');
vo(run)(function(err, result) {
if (err) throw err
console.log("Hi");
})
function *run() {
var nightmare = Nightmare({show:true});
console.log("1st step *run");
yield nightmare
.goto('https://www.myskills.gov.au/courses/search/')
.wait(12000)
//.click('#select3 value="100")
.evaluate(function () {
var hrefs = [];
$('.search-result h4 a').each(function()
{
var course = new Object();
course.title = $(this).html();
course.link = $(this).attr('href');
course.code = $('.search-result .col-md-2.text-small
span').html();
hrefs.push(course);
});
return hrefs;
})
.then(function(str){
console.log("2nd step evaluate then on page load");
console.log(str);
for(var i=0; i< 8; i++)
{
console.log(i);
sleep.sleep(1);
var link = "https://www.myskills.gov.au";
var coursetitle = str[i].title
var courselink = link +str[i].link+"\n";
var coursesinglelink = link +str[i].link;
var courseData = coursetitle+"\n"+str[i].code+"\n"+courselink;
fs.appendFile('getcourselink.txt', courseData, function (err) {
if(err) console.log(err);
});
vo(run1)(function(err, result) {
if (err) console.log ("err: ",err);
console.log("Hi in run1");
//console.log(result);
});
function *run1() {
console.log("I 2nd time:-"+i);
console.log(coursesinglelink);
sleep.sleep(2);
var nightmare1 = Nightmare({show:true});
yield nightmare1
.goto(coursesinglelink)
.wait(9000)
.evaluate(function () {
var str="Hi";
var CourseDetails = $('#details #courseStructureDiv
#packagingrules').text();
str = str+"\n"+CourseDetails;
return str;
})
.then(function(str){
console.log("Run inner then",str);
fs.appendFile('getcourselink.txt', str, function (err) {
if(err) console.log(err);
});
// nightmare1.end();
});
}
//nightmare.end();
// nightmare.proc.disconnect();
// nightmare.proc.kill();
// nightmare.ended = true;
// nightmare = null;
}
});
}

Setting $scope.items in Angular Binding

I have a service with the following function,
public object Get(AllUsers request)
{
var users = XYZ.GetAllUsers();
var userList = users.Cast<XYZ>();
return new AllUsers
{
UsersAcc = userList.Select(ConvertToEntity).ToList()
};
}
I am trying to get the results from angular controller.
function UserAccountController($scope, $location, $filter, UserAccount) {
#scope.items = function(){
var abc = UserAccount.query();
return abc.UsersAcc
}
}
Here is my Service
angular.module('userAccService', ['ngResource']).factory('UserAcc', function($resource) {
return $resource('/api/useracc/:id', {}, {
query: {
method: 'GET',
}
});
I am new to angular service, and can't seem to make it to work.
You need to create an array object and return it. After the query is done you can populate that same instance with the list UsersAcc. Keep in mind that $scope.items will be [] untill the query returns with data.
$scope.items = getUsersAcc();
function getUsersAcc() {
var dataArray = new Array();
UserAccount.query(function (data) {
var list = data.UsersAcc;
for (var i = 0, c = list.length; i < c; i++) {
dataArray.push(list[i]);
}
};
return dataArray;
};

Resources