Mongodb count the number of a field with certain condition - count

This is the data I have in the database:
{
"_id" : ObjectId("5bf84d5eb6655873af59ead6"),
"game" : 1.0,
"action" : {
"actionType" : "GameStart",
"actionNumber" : 0.0,
"Player1" : {
"user" : 2.0,
"name" : "Kevin"
},
"Player2" : {
"user" : 4.0,
"name" : "Sue"
}
}
}
The question is to report the total number of started games. I tried this code db.hw6.count({'action.actionType': "GameStart" }), and got an error. I have no idea how I did it wrong. can anyone help?

Please use itcount() instead of count to solve the error.
It is because you are using Azure CosmosDB rather than MongoDB. count() will work for mongodb but in your case you should use itcount() which is CosmosDB implementation.
Please refer mongodb documentation. https://docs.mongodb.com/manual/reference/method/cursor.itcount/
Below error will be resolved after using itcount()******
Error: count failed: { "_t" : "OKMongoResponse", "ok" : 0, "code" : 13, "errmsg" : "Cannot execute command ExecuteJavaScript using PrimaryReadonlyMasterKey", "$err" : "Cannot execute command ExecuteJavaScript using PrimaryReadonlyMasterKey" } : _getErrorWithCode#src/mongo/shell/utils.js:25:13 DBQuery.prototype.count#src/mongo/shell/query.js:383:11 DBCollection.prototype.count#src/mongo/shell/collection.js:1700:12 #(shell):

Related

JFrog Artifactory AQL - Pagination mechanism

I am currently trying to query Artifactory for 25 results.
So I used:
items.find({"name" : {"$match":"somefile*"}).limit(25)
But I need to get also the Total result for calculate Total results.
If there is 500 results, i want to get it also.
For Example:
500/25 = 20 pages.
You can use the search command of the JFrog CLI, with the --count option to only get the number of results.
Or, using AQL through the REST API, you can first query without limit to get the total number of results, by looking at the total count at the range field:
{
"results" : [
{
"repo" : "libs-release-local",
"path" : "org/jfrog/artifactory",
"name" : "artifactory.war",
"type" : "item type",
"size" : "75500000",
"created" : "2015-01-01T10:10;10",
"created_by" : "Jfrog",
"modified" : "2015-01-01T10:10;10",
"modified_by" : "Jfrog",
"updated" : "2015-01-01T10:10;10"
}
],
"range" : {
"start_pos" : 0,
"end_pos" : 1,
"total" : 1 // <----
}
}

How to get Kibana dashboard ID?

How can I get kibana dashboard ID? for API call to export data from the dashboard.
I searched everywhere but I can't find ID for dashboard like in example(The dashboard ID is 942dcef0-b2cd-11e8-ad8e-85441f0c2e5c.).
I'm using ELK stack 7.4.1 OSS(Community version).
You can query your .kibana index from Command Line in your Elasticsearch with something like this;
$ curl -s http://localhost:9200/.kibana/dashboard/_search?pretty
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.0,
"hits" : [ {
"_index" : ".kibana",
"_type" : "dashboard",
"_id" : "New-Dashboard",
"_score" : 1.0,
"_source" : {
"title" : "New Dashboard",
"hits" : 0,
"description" : "",
"panelsJSON" : "[{\"id\":\"Visualization-VerticalBarChart\",\"type\":\"visualization\",\"panelIndex\":1,\"size_x\":3,\"size_y\":2,\"col\":1,\"row\":1}]",
"optionsJSON" : "{\"darkTheme\":false}",
"uiStateJSON" : "{}",
"version" : 1,
"timeRestore" : false,
"kibanaSavedObjectMeta" : {
"searchSourceJSON" : "{\"filter\":[{\"query\":{\"query_string\":{\"query\":\"*\",\"analyze_wildcard\":true}}}]}"
}
}
} ]
}
}
Or if you want individual ones you can include the title or the id;
curl -s 'http://localhost:9200/.kibana/dashboard/_search?pretty=1,q=_id=New-Dashboard'
You may refer https://www.elastic.co/guide/en/kibana/current/index.html for more reference
You can find the ID in the URL.
Open the dashboard in Kibana. Extract the part in the URL after <view/> and before <?>.
For http://localhost:5601/app/dashboards#/view/bd1cc200-1169-4ee3-90da-c08d7eaacab5?_g=(filters:!()... the ID is bd1cc200-1169-4ee3-90da-c08d7eaacab5.
API
Alternative you can search and find dashboards using the saved objects API in Kibana:
curl http://localhost:5601/api/saved_objects/_find?type=dashboard
(Using Kibana 7.16)

MongoDB query returns "Error: Invalid JSON object" on valid query

Issue
I have probably mucked up the syntax for the JSON query, but cannot for the life of me see where. Trying to create data.frame overviewData grouped on four values with an extra COUNT-feature.
Code
overviewData <- M_CONNECTION$aggregate('[
{
"$group" : {
"_id" : {
"Hotel_Name" : "$Hotel_Name",
"lat" : "$lat",
"lng" : "$lng",
"Average_Score" : "$Average_Score"
},
"COUNT(Hotel_Name)" : {
"$sum" : NumberInt(1)
}
}
},
{
"$project" : {
"Hotel_Name" : "$_id.Hotel_Name",
"lat" : "$_id.lat",
"lng" : "$_id.lng",
"Average_Score" : "$_id.Average_Score",
"COUNT(Hotel_Name)" : "$COUNT(Hotel_Name)",
"_id" : NumberInt(0)
}
}
]',
options = '{"allowDiskUse" : true}'
)
I have quotes around all of my queries, as well as around the options. Still getting an "Invalid JSON object"-error message.
Though I'm not sure, I guess this line is causing you trouble ->
"$sum" : NumberInt(1)
Instead of this, please try this ->
"$sum" : 1
Kindly try this and let me know if this helped.

Artifactory aql: find builds of job with given property

I am trying to query which build number(s) produced artifacts from build foo with artifact property vcs.Revision=aabbccddee123456.
In Artifactory 5.1.3.
I was trying like this so far:
curl -u user:apikey -i -X POST https://artifactory.foobar.com/artifactory/api/search/aql -H "content-type:text/plain" -T query.json
query.json:
builds.find(
{
"module.artifact.item.repo":"snapshot-local",
"name":"foo",
"module.artifact.item.#vcs.Revision":"aabbccddee123456"
}
)
However, none of these 3 lines seem individually correct:
builds.find({"module.artifact.item.repo":"snapshot-local"})
returns nothing,
builds.find({"name":"foo"})
returns the same empty response,
builds.find({"module.artifact.item.#vcs.Revision":"aabbccddee123456"}) also returns this:
{
"results" : [ ],
"range" : {
"start_pos" : 0,
"end_pos" : 0,
"total" : 0
}
}
What am I doing wrong here? I do see in the webapp the builds I published with this name, and with the correct artifact properties.
Here's a working solution that will give build numbers (since giving admin rights to query builds is not a solution for us):
query.json:
items.find(
{
"repo":"snapshot-local",
"artifact.module.build.name":"foo",
"artifact.item.#vcs.Revision":"aabbccddee123456"
}
).include("artifact.module.build.number")
This returns a list of all the artifacts that were built with the relevant properties, with the build number attached, e.g:
{
"results" : [ {
"repo" : "snapshot-local",
"path" : "foo/42",
"name" : "a.out",
"type" : "file",
"size" : 123456789,
"created" : "2018-07-05T12:34:56.789+09:00",
"created_by" : "jenkins",
"modified" : "2018-07-05T12:34:56.789+09:00",
"modified_by" : "jenkins",
"updated" : "2018-07-05T12:34:56.789+09:00",
"artifacts" : [ {
"modules" : [ {
"builds" : [ {
"build.number" : "42"
} ]
} ]
} ]
},
[SNIP]
}
],
"range" : {
"start_pos" : 0,
"end_pos" : 30,
"total" : 30
}
}
I can then parse this to extract build.number.
Certain AQL queries requires a user with admin permissions.
To ensure that non-privileged users do not gain access to information without the right permissions, users without admin privileges have the following restrictions:
The primary domain in the query may only be item.
The following three fields must be included in the include directive: name, repo, and path.
In your case, you are using the build domain in the query which requires admin permissions

Querying nested values with Q4 2015 version 1.1.3

Based on Tom's answer to this question, I'm getting the following error when trying to query a nested field on a collection.
I'm trying to get a single user object with personalityExams.id.extId = 24232. I'd like to know the best way to query this, and how to form an index rule for it.
Here is what the user looks like in firebase
{
"users": {
"93306b91-d5ba-4e06-838c-0ab85fd58783": {
"createdOn" : 1451495976870,
"email" : "iyad.bitar#gmail.com",
"firstName" : "Iyad",
"lastModifiedOn" : 1451590413654,
"lastName" : "Bitar",
"mobile" : "34234333",
"provider" : "password",
"userType" : "JS",
"personalityExams": [
{
"extId": "24232",
"skills": ["teaching"]
}
]
}
In my javascript, I'm trying to get it like so:
(new Firebase("https://hirely-dev.firebaseio.com/users")).orderByValue("personalityExams/extId").equalTo("24232").on("value", function(snapshot) {user1 = snapshot.val();});
Error:
Uncaught Error: Query.orderByValue failed: Was called with 1 argument. Expects none.
at Error (native)
at x (http://localhost:7200/lib/firebase/firebase.js:20:1074)
at U.g.Fg (http://localhost:7200/lib/firebase/firebase.js:233:343)
at <anonymous>:2:59
at Object.InjectedScript._evaluateOn (<anonymous>:875:140)
at Object.InjectedScript._evaluateAndWrap (<anonymous>:808:34)
at Object.InjectedScript.evaluate (<anonymous>:664:21)

Resources