Appsync subscription filtering issue when filtering lists using IN operator and dynamic values - aws-appsync

With the following type:
type Task {
id: ID
owner: String
title: String
description: String
taskStatus: String
priority: Priority
department: String
classification: Int
ownerIds: [String]
}
enum Priority {
none
lowest
low
medium
high
highest
}
Below Subscription filter does't seem to work with a property of ownerIds: [String].
If I add a record with ownerIds foo it will not show me the result.
#set($userId = $ctx.identity.claims["cognito:username"])
$extensions.setSubscriptionFilter({
"filterGroup": [
{
"filters" : [
{
"fieldName": "ownerIds",
"operator": "in",
"value": ["foo"]
}
]
}
]
})
$util.toJson($context.result)
When I change the filter fieldName to priority and the value is high, it works as expected.
Does the in operator only work with Enums and not String arrays?

Related

Execute Logical Operator Filters On GraphQL OnlyOn JSON Objects

Thank you for help. I am trying to execute AND/OR operator in GraphQL without Database.
Below is Query need to execute on dataset not database. Please understand, I don't have authority to connect to any database.
{
customVisualsData(_filter: {and: [{expression: {field: "Country", like: "Canada"}},{expression: {field: "Profit", gt: "5000"}}]}) {
Country
DiscountBand
Product
Segment
Profit
}
}
Transform dataset/JSON Object look like this.
[
{
"Country": "Canada",
"DiscountBand": "High",
"Product": "Paseo",
"Segment": "Government",
"COGS": 1477815,
"GrossSales": 2029256,
"ManufacturingPrice": 70,
"UnitsSold": 12230.5,
"Profit": 300289.99999999994
},
{
"Country": "United States of America",
"DiscountBand": "High",
"Product": "VTT",
"Segment": "Small Business",
"COGS": 1461250,
"GrossSales": 1753500,
"ManufacturingPrice": 750,
"UnitsSold": 5845,
"Profit": 74288
}
]
Schema Builder, I used to create GraphQL Query builder.
var schema = buildSchema(`
type customVisualObject {
Country: String
DiscountBand: String
Product: String
Segment: String
COGS: Float
GrossSales: Float
ManufacturingPrice: Int
UnitsSold: Float
Profit: Float
}
type Query {
customVisualsData(_filter: FilterInput): [customVisualObject]
}
input FilterExpressionInput {
field: String!
eq: String
gt: String
gte: String
like: String
}
input FilterInput {
expression: FilterExpressionInput
and: [FilterInput!]
or: [FilterInput]
not: [FilterInput!]
}
`);
Please let me know, if anyone know How to set resolver for this on graphQL?
Does anyone one know JSON-ata Or GraphQL library to execute such complex Query on JSON Object Not databse?
I appreciate your help.

Mongodb multilingual search: Which schema is better for faster search results - nested or having language specific fields directly

We are implementing fuzzy search on product using Atlas search index and for querying, we are using Mongoose. The kind of search we want includes multilingual searching and for this we are using following schema for the product -
{
language: "de",
name: String,
description: String,
translation: {
en: {
name: String,
description: String
},
fr: {
name: String,
description: String
}
}
}
Will above schema be a good fit considering search performance as there will be thousands or more hits for reading the data. Going forward, the search queries may go up to millions as it is an e-commerce system. Having nested structure will be good for querying or there are another options we can opt for,
Having language specific fields directly with shorthand specified for language:
{
name_de: String,
description_de: String,
name_en: String,
description_en: String,
name_fr: String,
description_fr: String
}
Having language specific fields nested with the field name as the key
{
name: {
en: String,
de: String,
fr: String
},
description: {
en: String,
de: String,
fr: String
}
}
Having language as the key and field names nested in that object:
{
en: {
name: String,
description: String
},
fr: {
name: String,
description: String
}
}
Or any other schema that will be suitable for this scenario?
Search will be performed on the basis of language selected by the user. So, if a user opts for French as his preferred language, we will look for the keyword typed by user in French language.
P.S. - There are more fields than just name and description which are also language specific.
I would opt for option one because of the limited support for nested fields in Atlas Search, though option 2 would work as well. Here is how I would define the index in your case:
{
"mappings": {
"fields": {
"name_de": {
"analyzer": "lucene.german",
"type": "string"
},
"name_fr": {
"analyzer": "lucene.french",
"type": "string"
},
"name_en": {
"analyzer": "lucene.english",
"type": "string"
},
"description_de": {
"analyzer": "lucene.german",
"type": "string"
},
"description_fr": {
"analyzer": "lucene.french",
"type": "string"
},
"description_en": {
"analyzer": "lucene.english",
"type": "string"
}
}
}
}
This way, you can the benefits of highlighting, which could be extra helpful if your description field is long. You will also get better stop word support and diacritics out of the box. If you have any trouble, let me know here and I will help.

Mongoose/MongoDB find Document with array of objects by multiple values

I have the following document structure:
recipients: [
{
name: String,
hidden: Boolean,
},
{
name: String,
hidden: Boolean,
},
// more ...
];
I want to query all documents for a given name and a given hidden value in the same object, meaning at the same index of the recipients array. How can I query for example "all documents for name = test and hidden = false" (where hidden is in the same object as the name)? I tried the following
const chats = await Model.find(
{
'recipients.name': name,
'recipients.hidden': false,
},
But this still returns the document because it does not seem to use those 2 conditions for the same object, but across all objects in the array.
Nevermind, got it. See the MongoDB docs for $elemMatch (https://docs.mongodb.com/manual/reference/operator/query/elemMatch/#array-of-embedded-documents)
{
"recipients": {
"$elemMatch": {
"name": name,
"hidden": false
},
},
}

MongoDB - Query nested objects in nested array with array of strings filter

So basically I need to filter my data with my own filter, which is array of strings, but problem is, that that exact field is inside nested object in array in DB. so, part of my Schema looks like this:
members: [
{
_id: { type: Schema.Types.ObjectId, ref: "Users" },
profilePicture: { type: String, required: true },
profile: {
firstName: { type: String },
lastName: { type: String },
occupation: { type: String },
gender: { type: String }
}
}
]
and my filter looks like this
gender: ["male","female"]
expected result with this filter is to get a team which has both male users and female users, if it has only male, or only female, it should not give me that team. but everything i've tried was giving me everything what included males and females even tho there were only male members.
what i've tried:
db.teams.find(members: { $elemMatch: { "profile.gender": { $in: gender } } })
This works only when there is one gender specified in the filter, and well, i know it must not work on what i am trying to achieve, but i dont know how to achieve it. any help will be appreciated
Edit: I've tried to do it in this way
db.teams.find({
$and: [
{ members: { $elemMatch: { "profile.gender": gender[0] } } },
{ members: { $elemMatch: { "profile.gender": gender[1] } } }
]
})
and this gives me result only when both filters are specified, however, if there is only one filter(either "male", or "female") it is giving me nothing.
Use $and operator instead of $in.
db.teams.find(members: {$elemMatch: {$and: [{'profile.gender': 'male'}, {'profile.gender': 'female'}]}})
This query works no matter how many elements you want to compare
db.teams.find({$and: [{'members.profile.gender': 'male'}, {'members.profile.gender': 'female'}]})
You need to dynamically generate the query before passing it to find, if you want to cover more than one case.
You can do this with the $all operator that finds docs where an array field contains contains all specified elements:
var gender = ['male', 'female'];
db.teams.find({'members.profile.gender': {$all: gender}});

Mongo find documents where value of property does not contains a given string

This Meteor server code needs to find all document where food does not contains 'hot' case insensitive.
FoodCol.find({food: /^hot/}); is not cutting it.
So that I need the code to only return {food: 'chicken soup, type: 'soups'} sine it is the only document where the string 'hot' is not found in the property 'food'.
How can it be done? Thanks
{
{
food: 'Hot coffee',
type: 'drink'
}, {
food: 'cake with hot topping',
type: 'cake'
}, {
food: 'chicken soup',
type: 'soups'
}
}
Run the following query, it uses the $not operator which will perform a logical NOT operation on the regex specified and selects the documents that do not match the regex:
FoodCol.find({ "food": { "$not": /hot/i } })