CosmosDB $elemMatch syntax error - azure-cosmosdb

I am getting a strange syntax error for some commands in the MongoDB API for CosmosDB. Say I have a collection called "Collection" with two documents:
{
"_id" : 1,
"arr" : [
{
"_id" : 11
},
{
"_id" : 12
}
]
}
{
"_id" : 2,
"arr" : [
{
"_id" : 21
},
{
"_id" : 22
}
]
}
If I try to run the query
db.getCollection('Collection').find( { _id : 2 }, { arr : { $elemMatch : { _id : 21 } } })
I get the result
{
"_t" : "OKMongoResponse",
"ok" : 0,
"code" : 9,
"errmsg" : "Syntax error, incorrect syntax near '10'.",
"$err" : "Syntax error, incorrect syntax near '10'."
}
But the command works perfectly fine on my locally hosted instance of MongoDB, returning the expected result:
{
"_id" : 2,
"arr" : [
{
"_id" : 21
}
]
}
Anyway, this is certainly not a syntax error, but there is no helpful error message. If this is not yet supported by CosmosDB, is there any way to only get certain embedded documents stored in an array?
If I try to use an aggregation pipeline to just extract the document in the array (I realize this should give a different result than the command above, but it would also work for my purposes), like so:
db.getCollection('Collection').aggregate([{ "$unwind" : "$arr" }, { "$match" : { "arr._id" : 21 } }] )
I get the result
{
"_t" : "OKMongoResponse",
"ok" : 0,
"code" : 118,
"errmsg" : "$match is currently only supported when it is the first and only stage of the aggregation pipeline. Please restructure your query to combine multiple $match stages into a single $match stage.",
"$err" : "$match is currently only supported when it is the first and only stage of the aggregation pipeline. Please restructure your query to combine multiple $match stages into a single $match stage."
}
So that doesn't work for me either.

Try this
db.collection.aggregate([
{
$match: {
"_id": 2
}
},
{
$project: {
arr: {
$filter: {
input: "$arr",
as: "ar",
cond: {
$eq: [
"$$ar._id",
21
]
}
}
}
}
}
])
Check it here

Related

Elasticsearch query using Kibana does not work using Java Rest Client API

Can someone help determine why a kibana query does not return hits when using the Elasticsearch Java Rest Client API.
I am currently using
Elasticsearch/Kibana: 7.16.2
Elasticsearch Java Client: 6.6.2
I am reluctant to upgrade java client due to numerous Geometry related updates needed.
fields:
mydatetime: timestamp of doc
category: keyword field
We have 1000 or more records for each category a day.
I want an aggregation that shows categories by day and includes the first and last "date" for the category.
This query works in Kibana
GET /mycategories/_search
{
"size":0,
"aggregations":{
"bucket_by_date":{
"date_histogram":{
"field":"mydatefield",
"format":"yyyy-MM-dd",
"interval":"1d",
"offset":0,
"order":{
"_key":"asc"
},
"keyed":false,
"min_doc_count":1
},
"aggregations":{
"unique_categories":{
"terms":{
"field":"category",
"size":10,
"min_doc_count":1,
"shard_min_doc_count":0,
"show_term_doc_count_error":false,
"order":[
{
"_count":"desc"
},
{
"_key":"asc"
}
]
},
"aggregations":{
"min_mydatefield":{
"min":{
"field":"mydatefield"
}
},
"max_mydatefield":{
"max":{
"field":"mydatefield"
}
}
}
}
}
}
}
}
The first record of the result...category1 and category2 for 2022 05 07 with min and max "mydatetime" for each category
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 4,
"successful" : 4,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2593,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"bucket_by_date" : {
"buckets" : [
{
"key_as_string" : "2022-05-07",
"key" : 1651881600000,
"doc_count" : 2,
"unique_missions" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "category1",
"doc_count" : 1,
"min_mydatefield" : {
"value" : 1.651967952E12,
"value_as_string" : "2022-05-07T13:22:17.000Z"
},
"max_mydatefield" : {
"value" : 1.651967952E12,
"value_as_string" : "2022-05-07T23:59:12.000Z"
}
},
{
"key" : "category2",
"doc_count" : 1,
"min_mydatefield" : {
"value" : 1.651967947E12,
"value_as_string" : "2022-05-07T03:47:23.000Z"
},
"max_mydatefield" : {
"value" : 1.651967947E12,
"value_as_string" : "2022-05-07T23:59:07.000Z"
}
}
]
}
},
I have successfully coded other, less complex aggregations without problems. However, i have not been able to get either an AggregationBuilder or WrapperQuery. Zero results are returned.
{"took":0,"timed_out":false,"_shards":{"total":0,"successful":0,"skipped":0,"failed":0},"hits":{"total":0,"max_score":0.0,"hits":[]}}
Before executing the query, i copy the SearchRequest.source() into Kibana, and it runs and returns the desired information.
Below is the AggregationBuilder code that seems to replicate my kibana query, but returns no results.
AggregationBuilder aggregation =
AggregationBuilders
.dateHistogram("bucket_by_date").format("yyyy-MM-dd")
.minDocCount(1)
.dateHistogramInterval(DateHistogramInterval.DAY)
.field("mydatefield")
.subAggregation(
AggregationBuilders
.terms("unique_categories")
.field("category")
.subAggregation(
AggregationBuilders
.min("min_mydatefield")
.field("mydatefield")
)
.subAggregation(
AggregationBuilders
.max("max_mydatefield")
.field("mydatefield")
)
);

Convert String to Int on AWS DocumentDB

I am currently trying to write a metabase question off AWS Document DB and I am running into an issue where I need to convert a string to an integer. Unfortunately, it seems like aws documentdb does not support $toInt and I am not sure how to get around it. Here is the query:
[
{"$match": {
"metaData.fileSize" : {"$exists": true}
}},
{"$project": {
"file_size" : "$metaData.fileSize",
"timestamp": 1,
"past7Days":
{ "$subtract":
[ ISODate(), 604800000]
}
}},
{"$project": {
"file_size" : 1,
"timestamp": 1,
"dayofweek": {"$dayOfWeek":["$timestamp"]},
"past7DaysComp":
{ "$subtract":
[ "$timestamp", "$past7Days"]
}
}},
{"$group" :
{
"_id" : {"dayofweek" : "$dayofweek"},
"size": {"$avg" : "$file_size"}
}
}
]
The group returns nothing for size since it is not of a numeric type. Any ideas how to convert file_size to integer or double or float?

Meteor - UserIsInRole always returning false

I Have added allaning:roles in my meteor-react project but
Roles.userIsInRole(Meteor.userId(), ["ADMIN"]) always returning false.
I have roles in my mongo as follows,
{ "_id" : "9B3D757wQA9Mz4RGt", "name" : "BUYER" }
{ "_id" : "PDCJH8D6JDo9fttFj", "name" : "ADMINKAMB" }
{ "_id" : "XFGsrfB3Xsm6F8LbQ", "name" : "SELLER" }
{ "_id" : "hDLSvdo6wMnF47BBz", "name" : "ADMIN" }
{ "_id" : "ADMINKAMB", "children" : [ ] }
if you remove the autopublish from yours packages, try to add this code
For my case, I create a file /imports/startup/server/publications/index.js
Meteor.publish(null, function() {
if (this.userId) {
return Meteor.roleAssignment.find({ 'user._id': this.userId });
}
return this.ready();
});
Then in server/main.js
import '/imports/startup/server/publications'

Example Dgraph recurse sum query

New Dgraph user wondering if anyone can provide me with an example recursive count and sum query to help get me going.
The data looks like this (there are more predicates, but left out for simplicity):
{
"uid" : <0x1>,
"url" : "example.com",
"link" : [
{
"uid" : <0x2>,
"url" : "example2.com",
"link" : [
{
"uid" : <0x4>,
"url" : "example4.com",
"link" : [
{
"uid" : <0x6>,
"url" : "example6.com",
"link" : [
{
etc...
}
]
}
]
},
{
"uid" : <0x5>,
"url" : "example5.com",
}
]
},
{
"uid" : <0x2>,
"url" : "example2.com",
"link" : [
{
etc ....
}
},
]
}
Just a home page with n-links which each have n-links and the depth, obviously, can vary. Just hoping for a good example of how to count all the links for each url and sum them up. I will add different filters to the query at some point, but just wanting to see a basic query to help get me going. Thanks.

Why dynamodb query() always say I am applying type M for BETWEEN operator?

I have get parameter like:
param = {
TableName: 'sizings',
KeyConditions: {
"user" : {
"ComparisonOperator" : "EQ",
"AttributeValueList" : [{"S":"xxxxxxxxxxxxx"}]
},/*
"ts" : {
"ComparisonOperator" : "BETWEEN",
"AttributeValueList" : [ { N : 0 } , { N : 100 } ]
}*/
},
select: 'SPECIFIC_ATTRIBUTES',
AttributesToGet:['user']
};
But the query will return an error:
ValidationException: One or more parameter values were invalid: ComparisonOperator BETWEEN is not valid for M AttributeValue type
Why It consider i specified M? Actually I specified type N for AttributeValue parameter.
Here is the correct syntax using BETWEEN.
var params = {
TableName : 'sizings',
KeyConditions: {
"user" : {
"ComparisonOperator" : "EQ",
"AttributeValueList" : [{"S":"d1"}]
},
"ts" : {
"ComparisonOperator" : "BETWEEN",
"AttributeValueList" : [ { "N" : "20170101" } , { "N" : "20171231" } ]
}
},
Select: 'SPECIFIC_ATTRIBUTES',
AttributesToGet: ['user']
};
Please note that you are using legacy parameters.
KeyConditions — (map) This is a legacy parameter. Use
KeyConditionExpression instead.
Without using legacy parameters:-
var params = {
TableName : 'tableName',
KeyConditionExpression : 'device_id = :deviceIdVal and timestampAttr between :t1 and :t2',
ExpressionAttributeValues : {
':deviceIdVal' : 'd1',
':t1' : 20170101,
':t2' : 20171231
}
};

Resources