Ravendb 2.5: query giving 0 results while it should be 1 - ravendb

I've got a problem with a query I can't figure out, I'm doing this query in code:
var userList = (from user in this.documentSession.Query<User>()
where user.FederatedUserIds[authenticatedClient.ProviderName] == authenticatedClient.UserInformation.Id
select user).ToList();
In this case the providername is facebook and the id = 100001103765630. The FederatedUserId's is a Dictionary.
Which results in this query to the server:
http://localhost:8080/indexes/dynamic/Users?&query=FederatedUserIds.facebook%3A100001103765630&pageSize=128
Which gives zero results, also from a query in the webbrowser:
{
"Results": [],
"Includes": [],
"IsStale": false,
"IndexTimestamp": "2013-08-24T14:52:44.0511623Z",
"TotalResults": 0,
"SkippedResults": 0,
"IndexName": "Auto/Users/ByFederatedUserIds_facebook",
"IndexEtag": "01000000-0000-0064-0000-000000000001",
"ResultEtag": "2BD9AA1E-935A-FEDF-3636-FAB0F155ED9E",
"Highlightings": {},
"NonAuthoritativeInformation": false,
"LastQueryTime": "2013-08-24T15:00:30.1200358Z",
"DurationMilliseconds": 1
}
While I have a document that's like this in the database, so I expect 1 result instead of 0:
{
"DisplayName": "neographikal",
"RealName": "x",
"Email": "x",
"PictureUri": "x",
"Roles": [
"User"
],
"ProfileImages": [],
"FederatedUserIds": {
"google": "x",
"twitter": "x",
"windowslive": "x",
"linkedin": "x",
"facebook": "100001103765630"
}
}
The strange thing is, this has never bothered me before in this piece of code. Can somebody see where I'm doing this wrong?

I was going to say that you might have stale results, but I see that "IsStale": false.
The only other thing I see is that the query on the URL comes through as FederatedUserIds.facebook while the field name is going to be FederatedUserIds_facebook in the index. However, I tested this and it worked, so it appears that the . is translated to _ before the query executes. I'm not sure when this was added or if it's always been that way.
Note if you try to query with . in Raven Studio, it doesn't work there, but _ does.
What build version are you running? I tested on 2.5.2666 and it worked for me.

Somehow, this was related to the indexes. Although it was a auto indexed query, deleting all the indexes on the server and rebooting the application solved the problem. I don't think this should have happened, but reproducing it seemed very difficult. If I can reproduce it in the feature, I'll try to investigate this further.
edit 01-09:
Found the problem and created a bug report: http://issues.hibernatingrhinos.com/issue/RavenDB-1334
Worked around it by creating a decent index:
public class UserByFederatedLoginIndex : AbstractIndexCreationTask<Core.Domain.User>
{
public UserByFederatedLoginIndex()
{
Map = users => from u in users
select new
{
u.DisplayName,
_ = u.FederatedUserIds.Select(x => CreateField("FederatedUserIds_"+x.Key, x.Value))
};
}

Related

Proper way to convert Data type of a field in MongoDB

Possible Replication of How to change the type of a field?
I am currently newly learning MongoDB and I am facing problem while converting Data type of field value to another data type.
Below is an example of my document
[
{
"Name of Restaurant": "Briyani Center",
"Address": " 336 & 338, Main Road",
"Location": "XYZQWE",
"PriceFor2": "500.0",
"Dining Rating": "4.3",
"Dining Rating Count": "1500",
},
{
"Name of Restaurant": "Veggie Conner",
"Address": " New 14, Old 11/3Q, Railway Station Road",
"Location": "ABCDEF",
"PriceFor2": "1000.0",
"Dining Rating": "4.4",
}]
Like above I have 12k documents. Notice the datatype of PriceFor2 is a string. I would like to convert the data type to Integer data type.
I have referred many amazing answers given in the above link. But when I try to run the query, I get .save() is not a function error. Please advice what is the problem.
Below is the code I used
db.chennaiData.find().forEach( function(x){ x.priceFor2= new NumberInt(x.priceFor2);
db.chennaiData.save(x);
db.chennaiData.save(x);});
This is the error I am getting..
TypeError: db.chennaiData.save is not a function
From MongoDB's save documentation:
Starting in MongoDB 4.2, the
db.collection.save()
method is deprecated. Use db.collection.insertOne() or db.collection.replaceOne() instead.
Likely you are having a MongoDB with version 4.2+, so the save function is no longer available. Consider migrate to the usage of insertOne and replaceOne as suggested.
For your specific scenario, it is actually preferred to do with a single update as mentioned in another SO answer. It only does one db call(while your approach fetches all documents in the collection to the application level) and performs n db call to save them back.
db.collection.update({},
[
{
$set: {
PriceFor2: {
$toDouble: "$PriceFor2"
}
}
}
],
{
multi: true
})
Mongo Playground

Handling multiple rows returned by IMPORTJSON script on GoogleSheets

I am trying to populate a google sheet using an API. But the API has more than one row to be returned for a single query. Following is the JSON returned by API.
# https://api.dictionaryapi.dev/api/v2/entries/en/ABANDON
[
{
"word": "abandon",
"phonetics": [
{
"text": "/əˈbændən/",
"audio": "https://lex-audio.useremarkable.com/mp3/abandon_us_1.mp3"
}
],
"meanings": [
{
"partOfSpeech": "transitive verb",
"definitions": [
{
"definition": "Cease to support or look after (someone); desert.",
"example": "her natural mother had abandoned her at an early age",
"synonyms": [
"desert",
"leave",
"leave high and dry",
"turn one's back on",
"cast aside",
"break with",
"break up with"
]
},
{
"definition": "Give up completely (a course of action, a practice, or a way of thinking)",
"example": "he had clearly abandoned all pretense of trying to succeed",
"synonyms": [
"renounce",
"relinquish",
"dispense with",
"forswear",
"disclaim",
"disown",
"disavow",
"discard",
"wash one's hands of"
]
},
{
"definition": "Allow oneself to indulge in (a desire or impulse)",
"example": "they abandoned themselves to despair",
"synonyms": [
"indulge in",
"give way to",
"give oneself up to",
"yield to",
"lose oneself in",
"lose oneself to"
]
}
]
},
{
"partOfSpeech": "noun",
"definitions": [
{
"definition": "Complete lack of inhibition or restraint.",
"example": "she sings and sways with total abandon",
"synonyms": [
"uninhibitedness",
"recklessness",
"lack of restraint",
"lack of inhibition",
"unruliness",
"wildness",
"impulsiveness",
"impetuosity",
"immoderation",
"wantonness"
]
}
]
}
]
}
]
By using the following calls via IMPORTJSON,
=ImportJSON(CONCATENATE("https://api.dictionaryapi.dev/api/v2/entries/en/"&$A2), "/phonetics/text", "noHeaders")
=ImportJSON(CONCATENATE("https://api.dictionaryapi.dev/api/v2/entries/en/"&$A2), "/meanings/partOfSpeech", "noHeaders")
=ImportJSON(CONCATENATE("https://api.dictionaryapi.dev/api/v2/entries/en/"&$A2), "/meanings/definitions/definition", "noHeaders")
=ImportJSON(CONCATENATE("https://api.dictionaryapi.dev/api/v2/entries/en/"&$A2), "/meanings/definitions/synonyms", "noHeaders")
=ImportJSON(CONCATENATE("https://api.dictionaryapi.dev/api/v2/entries/en/"&$A2), "/meanings/definitions/example", "noHeaders")
I am able to get the following in GoogleSheets,
Whereas, the actual output according to JSON should be,
As you can see a complete row is being overwritten. How can this be fixed?
EDIT
Following is the link to sheet for viewing only.
I believe your goal as follows.
You want to achieve the bottom image in your question on Google Spreadsheet.
Unfortunately, I couldn't find the method for directly retrieving the bottom image using ImportJson. So in this answer, I would like to propose a sample script for retrieving the values you expect using Google Apps Script. I thought that creating a sample script for directly achieving your goal might be simpler rather than modifying ImportJson.
Sample script:
function SAMPLE(url) {
var res = UrlFetchApp.fetch(url, {muteHttpExceptions: true});
if (res.getResponseCode() != 200) return res.getContentText();
var obj = JSON.parse(res.getContentText());
var values = obj[0].meanings.reduce((ar, {partOfSpeech, definitions}, i) => {
definitions.forEach(({definition, example, synonyms}, j) => {
var v = [definition, Array.isArray(synonyms) ? synonyms.join(",") : synonyms, example];
var phonetics = obj[0].phonetics[i];
ar.push(j == 0 ? [(phonetics ? phonetics.text : ""), partOfSpeech, ...v] : ["", "", ...v]);
});
return ar;
}, []);
return values;
}
When you use this script, please put =SAMPLE(CONCATENATE("https://api.dictionaryapi.dev/api/v2/entries/en/"&$A2)) to a cell as the custom formula.
Result:
When above script is used, the following
Note:
In this sample script, when the structure of the JSON object is changed, it might not be able to be used. So please be careful this.
References:
Class UrlFetchApp
Custom Functions in Google Sheets

how to filter Defect search by active projects in Rally using Web API

I built a custom search tool to allow searching Rally via the Web API from other applications and I've run into an issue. Right now I am allowing defects to be searched but I noticed that defects are coming back in the search results that are related to a project that is closed. I need to filter these out. I am wondering if there is a way to access attributes on a referenced object when querying another object, for example, if I have a query to search for defects where the name contains some text, such as https://rally1.rallydev.com/slm/webservice/v2.0/defect?query=(Name contains "keyword"), can I include something in that query to say that I only want defects for open projects by using the Project attribute on Defect, such as Project.State equals "Open". Basically I'm wondering if there is a way to do it in one query in an OData-ish format. Or as an alternative, if I separately query for a list of all open projects, could I add conditions to the query to say something like (Name contains "keyword") AND (ProjectId = ... OR ProjectId OR ...)? Any thoughts or suggestions are much appreciated.
A query for defects (or any other work item types) is not expected to return items from closed projects. WS API queries do not search closed projects.
Created a defect in a project. It happens to have FormattedID DE529
Tested (FormattedID = DE529) in WS API.
This json was returned:
{
QueryResult: {
_rallyAPIMajor: "2",
_rallyAPIMinor: "0",
Errors: [ ],
Warnings: [ ],
TotalResultCount: 1,
StartIndex: 1,
PageSize: 20,
Results: [
{
_rallyAPIMajor: "2",
_rallyAPIMinor: "0",
_ref: "https://rally1.rallydev.com/slm/webservice/v2.0/defect/36182496495",
_refObjectUUID: "aa35839a-5e49-44c6-8be7-2fb17bbd91bf",
_refObjectName: "bad defect",
_type: "Defect"
}
]
}
}
Closed the project. Ran the same query:
No result:
{
QueryResult: {
_rallyAPIMajor: "2",
_rallyAPIMinor: "0",
Errors: [ ],
Warnings: [ ],
TotalResultCount: 0,
StartIndex: 1,
PageSize: 20,
Results: [ ]
}
}
Also, it is not possible to query Projects by State. This query will return 0 results even when there are closed projects in the workspace 1234:
https://rally1.rallydev.com/slm/webservice/v2.0/project?workspace=https://rally1.rallydev.com/slm/webservice/v2.0/workspace/12345&query=(State = Closed)
Project names in Rally do not have to be unique. Identifying a project by Name may produce a misleading result in a corner case when you have two projects with the same name(one is Open, the other is Closed).

YouTube Search API gives me less videos than requested per page, but also gives me a nextPageToken

The query below (First Request) says there are 24 results, but only returns me 3 items (when I asked for 20 per page)... fine... but why is it also giving me a nextPageToken. I thought, well, maybe if I get less than the 20 I'm asking for, I'm at the end...
But... in another case below (Second and Third Requests) it said there were 1717 videos and it gave me the first 20, but when I got the next page it only gave me 5... also with a nextPageToken.
Note: this does include a location and radius parameter... maybe this is not quite baked yet.
The question, then, is how can I tell when I have reached the end of my results?
(Edit: I found the reason for getting only 5 was that you still DO need to include maxResults along with nextPageToken, so maybe the answer is simply you're at the end if the nextPageToken is null or the number of items is less than resultsPerPage.)
First Request
https://www.googleapis.com/youtube/v3/search?maxResults=20&type=video&order=rating&q=Stanford%20University&location=37.86854355%2C-122.502038433&locationRadius=6000m&key=AIzaSyDIOVCAngpI-xPkb30W5c6ee0PSBV9KbF8&part=id%2Csnippet
{
"nextPageToken": "CBQQAA",
"pageInfo": {
"totalResults": 24,
"resultsPerPage": 20
},
"items": [
{
"id": {
"kind": "youtube#video",
"videoId": "WMfwyjJz9hs"
},
},
{
"id": {
"kind": "youtube#video",
"videoId": "aGnYxoGHYHQ"
},
},
{
"id": {
"kind": "youtube#video",
"videoId": "s50ZSCKA1zY"
},
}
]
}
Second and Third Requests
https://www.googleapis.com/youtube/v3/search?maxResults=20&type=video&order=rating&q=Sausalito&location=37.8590937%2C-122.4852507&locationRadius=6000m&key=AIzaSyDIOVCAngpI-xPkb30W5c6ee0PSBV9KbF8&part=id%2Csnippet
https://www.googleapis.com/youtube/v3/search?pageToken=CBQQAA&key=AIzaSyDIOVCAngpI-xPkb30W5c6ee0PSBV9KbF8&part=id%2Csnippet
I have similar trouble. I fixed bug by deleting field &order=rating in request. I think this parameter exclude videos with Zero(0) rating / this url return more results https://www.googleapis.com/youtube/v3/search?maxResults=20&type=video&order=rating&q=Sausalito&location=37.8590937%2C-122.4852507&locationRadius=6000m&key=AIzaSyDIOVCAngpI-xPkb30W5c6ee0PSBV9KbF8&part=id%2Csnippet
Default order parameter value is 'relevance' returns similar result or you can try &order=viewCount parameter return similar result to

How does Raven DB SQL Replication handle saving arrays to SQL?

I am currently setting up SQL Replication to replicate our Raven DB documents into SQL for reporting purposes. So far everything has been working great. However, I am now trying to save a document that contains an array for days of the week.
This is how the document looks in Raven:
{
"ClientId": "clients/385",
"Description": "Test",
"IsOneOff": false,
"RecursEveryWeeks": 1,
"StartDate": "2013-03-19T00:00:00.0000000",
"TaskStartTime": "12:00:00",
"TaskDuration": 120,
"TaskEndTime": "14:00:00",
"AdditionalResources": false,
"AdditionalVisitType": "TestType",
"BillableTo": "Private",
"RecurrenceEndDate": "2013-04-30T00:00:00.0000000",
"DaysOfWeek": [
"Monday",
"Tuesday",
"Wednesday",
"Friday",
"Saturday"
]
}
In SQL Replication I have done the following:
sqlReplicate("AdditionalVisit", "AdditionalVisitId", {
ClientId: this.ClientId,
Description: this.Description,
IsOneOff: this.IsOneOff,
RecursEveryWeeks: this.RecursEveryWeeks,
StartDate: this.StartDate,
TaskStartTime: this.TaskStartTime,
TaskDuration: this.TaskDuration,
TaskEndTime: this.TaskEndTime,
AdditionalResources: this.AdditionalResources,
AdditionalVisitType: this.AdditionalVisitType,
BillableTo: this.BillableTo,
RecurrenceEndDate: this.RecurrenceEndDate,
DaysOfWeek: this.DaysOfWeek
});
All of this works fine when I leave DaysOfWeek out of the SQL Replication but causes the server to crash when I leave in.
How should this be done in SQL Replication so everything in the array is saved to a DaysOfWeek column in SQL?
I've not tested this, but it's along the lines of what you want...just add this to the end of your current script.
for (var i=0; i<this.DaysOfWeek.length; i++) {
var day = this.DaysOfWeek[i];
sqlReplicate('AdditionalVisit_DaysOfWeek', 'AdditionalVisitId', {
AdditionalVisitId: documentId,
DayOfWeek: day,
});
}
By the way there is currently a bug in the SqlReplication for RavenDb 2.1 where deletes won't be pushed through to SqlReplication, it's supposed to be fixed in 2.5 branch but there are still some other issues that need to be worked on for it to become usable.