Can this query be changed to add an or? - google-app-maker

I'm not sure how to change this query in google app maker language
var query = app.models.folders.newQuery();
query.filters.requestId._equals = request;
query.filters.filecount._equals = null;
var total = 0;
var recx = query.run();
So right now it is the equivalent of
select * from folders
where requestID = request
and filecount = null
But I would like to know if there is a way to change it to the equivalent of
select * from folders
where requestID = request
and (filecount = null or filecount < 0)
Maybe by using query.where?

You could try by just adding another filter to your query:
var query = app.models.folders.newQuery();
query.filters.requestId._equals = request;
query.filters.filecount._equals = null;
query.filters.filecount._lessThan = 0;
var total = 0;
var recx = query.run();

Related

Cosmosdb store procedure get less documents than real

I am preparing store procedure on cosmosdb by Javascript, however, it gets less documents than the real number of documents in collection.
The sproc is called by C#, C# pass a parameter "transmitterMMSI" which is also the partition key of this collection.
First, the following query is executed in sproc:
var query = 'SELECT COUNT(1) AS Num FROM AISData a WHERE a.TransmitterMMSI="' + transmitterMMSI + '"';
The result is output in response, and the value is 5761, which is the same as the real number of documents in collection.
However, when I change the query to the following:
var query = 'SELECT * FROM AISData a WHERE a.TransmitterMMSI="' + transmitterMMSI + '"';
The documents.length is output as 5574, which is smaller than the real number.
I have already changed the pageSize: -1, which should mean unlimited.
I did some search with google and stack overflow, it seems that continuation can be help. However, I tried some examples, and they don't work.
Anyone familiar with this can help?
The following list the scripts.
The sproc js script is here, which is also the file "DownSampling.js" used in the C# code:
function DownSampling(transmitterMMSI, interval) {
var context = getContext();
var collection = context.getCollection();
var response = context.getResponse();
var receiverTime;
var tempTime;
var groupKey;
var aggGroup = new Object();
var query = 'SELECT * FROM AISData a WHERE a.TransmitterMMSI="' + transmitterMMSI + '"';
var accept = collection.queryDocuments(collection.getSelfLink(), query, { pageSize: -1},
function (err, documents, responseOptions) {
if (err) throw new Error("Error" + err.message);
// Find the smallest deviation comparting to IntervalTime in each group
for (i = 0; i < documents.length; i++) {
receiverTime = Date.parse(documents[i].ReceiverTime);
tempTime = receiverTime / 1000 + interval / 2;
documents[i].IntervalTime = (tempTime - tempTime % interval) * 1000;
documents[i].Deviation = Math.abs(receiverTime - documents[i].IntervalTime);
// Generate a group key for each group, combinated of TransmitterMMSI and IntervalTime
groupKey = documents[i].IntervalTime.toString();
if (typeof aggGroup[groupKey] === 'undefined' || aggGroup[groupKey] > documents[i].Deviation) {
aggGroup[groupKey] = documents[i].Deviation;
}
}
// Tag the downsampling
for (i = 0; i < documents.length; i++) {
groupKey = documents[i].IntervalTime;
if (aggGroup[groupKey] == documents[i].Deviation) {
documents[i].DownSamplingTag = 1;
} else {
documents[i].DownSamplingTag = 0;
}
// Remove the items that are not used
delete documents[i].IntervalTime;
delete documents[i].Deviation;
// Replace the document
var acceptDoc = collection.replaceDocument(documents[i]._self, documents[i], {},
function (errDoc, docReplaced) {
if (errDoc) {
throw new Error("Update documents error:" + errDoc.message);
}
});
if (!acceptDoc) {
throw "Update documents not accepted, abort ";
}
}
response.setBody(documents.length);
});
if (!accept) {
throw new Error("The stored procedure timed out.");
}
}
And the C# code is here:
private async Task DownSampling()
{
Database database = this.client.CreateDatabaseQuery().Where(db => db.Id == DatabaseId).ToArray().FirstOrDefault();
DocumentCollection collection = this.client.CreateDocumentCollectionQuery(database.SelfLink).Where(c => c.Id == AISTestCollectionId).ToArray().FirstOrDefault();
string scriptFileName = #"..\..\StoredProcedures\DownSampling.js";
string scriptId = Path.GetFileNameWithoutExtension(scriptFileName);
var sproc = new StoredProcedure
{
Id = scriptId,
Body = File.ReadAllText(scriptFileName)
};
await TryDeleteStoredProcedure(collection.SelfLink, sproc.Id);
sproc = await this.client.CreateStoredProcedureAsync(collection.SelfLink, sproc);
IQueryable<dynamic> query = this.client.CreateDocumentQuery(
UriFactory.CreateDocumentCollectionUri(DatabaseId, AISTestCollectionId),
new SqlQuerySpec()
{
//QueryText = "SELECT a.TransmitterMMSI FROM " + AISTestCollectionId + " a",
QueryText = "SELECT a.TransmitterMMSI FROM " + AISTestCollectionId + " a WHERE a.TransmitterMMSI=\"219633000\"",
}, new FeedOptions { MaxItemCount = -1, EnableCrossPartitionQuery = true, MaxDegreeOfParallelism = -1, MaxBufferedItemCount = -1 });
List<dynamic> transmitterMMSIList = query.ToList(); //TODO: Remove duplicates
Console.WriteLine("TransmitterMMSI count: {0}", transmitterMMSIList.Count());
HashSet<string> exist = new HashSet<string>();
foreach (var item in transmitterMMSIList)
{
//int transmitterMMSI = Int32.Parse(item.TransmitterMMSI.ToString());
string transmitterMMSI = item.TransmitterMMSI.ToString();
if (exist.Contains(transmitterMMSI))
{
continue;
}
exist.Add(transmitterMMSI);
Console.WriteLine("TransmitterMMSI: {0} is being processed.", transmitterMMSI);
var response = await this.client.ExecuteStoredProcedureAsync<string>(sproc.SelfLink,
new RequestOptions { PartitionKey = new PartitionKey(transmitterMMSI) }, transmitterMMSI, 30);
string s = response.Response;
Console.WriteLine("TransmitterMMSI: {0} is processed completely.", transmitterMMSI);
}
}
private async Task TryDeleteStoredProcedure(string collectionSelfLink, string sprocId)
{
StoredProcedure sproc = this.client.CreateStoredProcedureQuery(collectionSelfLink).Where(s => s.Id == sprocId).AsEnumerable().FirstOrDefault();
if (sproc != null)
{
await client.DeleteStoredProcedureAsync(sproc.SelfLink);
}
}
I tried to comment the 2 loops in the JS codes, only the documents.length output, while the response number is still less. However, I changed the query to SELECT a.id, the documents.length is correct. Looks like it is the continuation issue.
The sproc is probably timing out. To use a continuation token in these circumstances, you will need to return it to your C# calling code then make another call to the sproc passing in your token. If you show us your sproc code we can help more.
You can use a continuation token to make repeated calls to queryDocuments() from within the sproc without additional roundtrips to the client. Keep in mind that if you do this too many times your sproc will eventually timeout, though. In your case, it sounds like you're already very close to getting all of the documents you're seeking so maybe you will be OK.
Here is an example of using a continuation token within a sproc to query multiple pages of data:
function getManyThings() {
var collection = getContext().getCollection();
var query = {
query: 'SELECT r.id, r.FieldOne, r.FieldTwo FROM root r WHERE r.FieldThree="sought"'
};
var continuationToken;
getThings(continuationToken);
function getThings(continuationToken) {
var requestOptions = {
continuation: continuationToken,
pageSize: 1000 // Adjust this to suit your needs
};
var isAccepted = collection.queryDocuments(collection.getSelfLink(), query, requestOptions, function (err, feed, responseOptions) {
if (err) {
throw err;
}
for (var i = 0, len = feed.length; i < len; i++) {
var thing = feed[i];
// Do your logic on thing...
}
if (responseOptions.continuation) {
getThings(responseOptions.continuation);
}
else {
var response = getContext().getResponse();
response.setBody("RESULTS OF YOUR LOGIC");
}
});
if (!isAccepted) {
var response = getContext().getResponse();
response.setBody("Server rejected query - please narrow search criteria");
}
}
}

when retrive data from mongodb it give the tiimeout exception

Bellow is my code
public async void TestLoops(long device, double date1, double date2)
{
var connectionString = "mongodb://localhost.:27017";//Connection string
MongoClientSettings settings1 = MongoClientSettings.FromUrl(new MongoUrl(connectionString));// Set Url to the MongoClient
//MongoS
// MongoClient mongoClient = new MongoClient(settings1);
var client = new MongoClient(new MongoClientSettings
{
Server = new MongoServerAddress("connectionString"),
ClusterConfigurator = builder =>
{
builder.ConfigureCluster(settings => settings.With(serverSelectionTimeout: TimeSpan.FromSeconds(1000)));
}
});
//Server s = new MongoServerAddress(connectionString);
var db = client.GetDatabase("tracking");//Specifing the Database name
var user = db.GetCollection<BsonDocument>("location");//Specifing t
var builder1 = Builders<BsonDocument>.Filter;
// var builder1 = Builders<BsonDocument>.Filter;
//int
var filt = builder1.Eq("device", device) & builder1.Gte("timestamp", date1) & builder1.Lte("timestamp", date2);
//var filter = builder1.Eq("device", 358740050124519);
var data = user.Find(filt).Count();
lblmsg.Text = data.ToString();
}
when i write the var data = user.Find(filt); it works fine
You can do
var collection = database.GetCollection<Type>("DBName");
var cursor = collection.Find(Query.EQ("FieldToMatch" : "ValueToMatch"));
var count = cursor.Count();
another approach
db.collection.CountAsync(); // you can pass new BsonDocument() or a condition to countAsync based on your requirement.

Retrieving CRM 4 entities with custom fields in custom workflow activity C#

I'm trying to retrieve all phone calls related to opportunity, which statecode isn't equal 1. Tried QueryByAttribute, QueryExpression and RetrieveMultipleRequest, but still has no solution.
Here some code i wrote.
IContextService contextService = (IContextService)executionContext.GetService(typeof(IContextService));
IWorkflowContext context = contextService.Context;
ICrmService crmService = context.CreateCrmService(true);
if (crmService != null)
{
QueryByAttribute query = new Microsoft.Crm.Sdk.Query.QueryByAttribute();
query.ColumnSet = new Microsoft.Crm.Sdk.Query.AllColumns();
query.EntityName = EntityName.phonecall.ToString();
query.Attributes = new string[] { "regardingobjectid" };
query.Values = new string[] { context.PrimaryEntityId.ToString() };
RetrieveMultipleRequest retrieve = new RetrieveMultipleRequest();
retrieve.Query = query;
retrieve.ReturnDynamicEntities = true;
RetrieveMultipleResponse retrieved = (RetrieveMultipleResponse)crmService.Execute(retrieve);
}
return ActivityExecutionStatus.Closed;
}
And almost same for QueryExpression
QueryExpression phCallsQuery = new QueryExpression();
ColumnSet cols = new ColumnSet(new string[] { "activityid", "regardingobjectid" });
phCallsQuery.EntityName = EntityName.phonecall.ToString();
phCallsQuery.ColumnSet = cols;
phCallsQuery.Criteria = new FilterExpression();
phCallsQuery.Criteria.FilterOperator = LogicalOperator.And;
phCallsQuery.Criteria.AddCondition("statuscode", ConditionOperator.NotEqual, "1");
phCallsQuery.Criteria.AddCondition("regardingobjectid", ConditionOperator.Equal, context.PrimaryEntityId.ToString();
I always get something like Soap exception or "Server was unable to proceed the request" when debugging.
To get exception details try to use following code:
RetrieveMultipleResponse retrieved = null;
try
{
retrieved = (RetrieveMultipleResponse)crmService.Execute(retrieve);
}
catch(SoapException se)
{
throw new Exception(se.Detail.InnerXml);
}

How to update dictionary in Swift located at every index of Array

This response is coming from server in JSON format
posts =
(
{
commentCount = 0;
"country_id" = 225;
"country_name" = "United States";
dislikeCount = 2;
disliked = 0;
firstname = Test;
imageHeight = 0;
imagePath = "";
imageWidth = 0;
lastname = Test;
likeCount = 2;
liked = 0;
postid = 126;
posttime = "2014-11-10 12:26:43";
shareCount = 0;
statusUpdate = Hello;
"upload_type" = text;
userId = 13;
userimage = "https://Test.com/1409976093.png";
videoHeight = "";
videoPath = "";
videoThumbnail = "";
videoWidth = "";
},
{
commentCount = 0;
"country_id" = 225;
"country_name" = "United States";
dislikeCount = 2;
disliked = 0;
firstname = Test;
imageHeight = 0;
imagePath = "";
imageWidth = 0;
lastname = Test;
likeCount = 3;
liked = 0;
postid = 125;
posttime = "2014-10-28 17:50:00";
shareCount = 0;
statusUpdate = "Happy%20chhata%20puja%20to%20you%20and%20your%20family%0Athanks%0ADr%20shah%20family";
"upload_type" = text;
userId = 78;
userimage = "https://Test.com/1410142960.png";
videoHeight = "";
videoPath = "";
videoThumbnail = "";
videoWidth = "";
}
Now I need to update the value of "liked" from 0 to 1 locally but I am not able to do that. I have stored this array in my array like this:
self.homePageDataArray.addObjectsFromArray(dictionary["posts"] as NSMutableArray)
And I am able to fetch the content from every index and also able to show this in TableView but I want to changed the status of "liked" from 0 to 1 when user will tap on the "Like Button" in my app. Obviously I will update this on server but I want to update that locally as well parallely.
Please let me know how it can be achieved. I tried a lot but every time it is showing some error! For Instance,
var tempDic:Dictionary<AnyObject,AnyObject> = self.homePageDataArray[selectedRow] as Dictionary
I am getting this error
Type 'AnyObject' does not conform to protocol 'Hashable'
Ultimately, I want to able to able to use the "updateValue" method so that I can update the value of my dictionary in my array.
This should work for you:
var tempDic = self.homePageDataArray[selectedRow] as Dictionary<String, AnyObject>
Your version:
var tempDic:Dictionary<AnyObject,AnyObject> = self.homePageDataArray[selectedRow] as Dictionary
Will not work because AnyObject is not conformable to Hashable, a requirement for the generic key type in Dictionary.
And to answer your other question:
var tempDic = self.homePageDataArray[selectedRow] as NSMutableDictionary as Dictionary
The statement above compiles because Dictionary is bridgeable to it's Objective-C counterpart; NSMutableDictionary. Your resultant dictionary will be of type Dictionary<String, AnyObject>. You're probably wondering how the type String was inferred. Well, NSMutableDictionary uses NSString as keys, a type which is bridgeable to it's Swift counterpart, you guessed it; String.
I really don't know WHY, but following solution worked for me
var tempDic = self.homePageDataArray[selectedRow] as NSMutableDictionary as Dictionary
If anyone can explain then it will help others also!

Bind dropdown to local (browser) database with HTML5/jQuery/ASP.NET

So I know how to store and retrieve data in a local database (Chrome). My function to retrieve data is
function SelectHandler(transaction, results) {
for (var i = 0; i < results.rows.length; i++) {
var row = results.rows.item(i);
var customer = new Object();
customer.id = row['id'];
customer.fname = row['fname'];
customer.lname = row['lname'];
}
}
I know how to assign the values from the function above to textboxes/labels etc but how do I create options in a dropdownlist with for example text first name and value id? I want to be able to select a name in the dropdown and then populate a textbox with the related last name (simplified example).
Thanks in advance.
for (var i = 0; i < results.rows.length; i++) {
var row = results.rows.item(i);
var newFeature = new Object();
newFeature.id = row['id'];
newFeature.name = row['name']
$('#ddlPatients').append(
$('<option></option>').val(newFeature.id).html(newFeature.name)
);
}

Resources