Using Wildcard with ISODate MongoDB - json

im trying to find all records that have a date that contains 00:00:00 in the hour minute and second portion to find documents that errored out in our subscription service. Was trying to use this but its erroring out.
db.CustomerSubscriptions.find({"UpcomingOrders.NextOrderDate": ISODate("/.*00:00:00.*/")})

You can use $dateToParts
db.CustomerSubscriptions.aggregate([
{
$set: {
dateParts: {$dateToParts: {date: "$UpcomingOrders.NextOrderDate", timezone: ... } }
}
},
{$match: {"dateParts.hour": 0, "dateParts.minute": 0, "dateParts.second": 0} }
])

You can only apply regEx on String, hence you can use aggregation and find the matching records. pl refer here : https://mongoplayground.net/p/4o2h98R5jve
Sample code:
db.collection.aggregate([
{
"$addFields": {
"dateStrMatched": {
$regexMatch: {
input: {
"$dateToString": {
"date": "$date"
}
},
regex: "00:00:00."
}
}
}
},
{
"$match": {
dateStrMatched: true
}
}
])
Pipeline stage:
Add field: convert date to string and match regex and store boolean flag as date matched or not
match: to filter only matched dates

Related

How to use lodash to filter objects by their property or the property of a subobject?

I have an object like this:
{
1234: {
name: 'biscuit-group',
groupMembers: {
9991: {
name: 'ryan'
},
9992: {
name: 'simon'
},
}
}
5678: {
name: 'taco-group'
groupMembers: {
9993: {
name: 'ryan'
},
9994: {
name: 'biscuitman'
},
}
}
}
With the search term biscuit it should filter by the group and groupMember names so that
if neither match, the group is removed
if the groupMember name matches then the group and that member are retained
if the group name but not the groupMember name matches then the group is retained without any members.
Expected result shown below:
{
1234: {
name: 'biscuit-group',
groupMembers: {}
}
5678: {
name: 'taco-group'
groupMembers: {
9994: {
name: 'biscuitman'
},
}
}
}
My idea is to use lodash to find the keys of the matching groups and matching members and then pick them from the original object, but this seems a bit longwinded.
How can I use lodash succinctly to filter objects by their property or the property of a subobject?

get difference between two dates in $project

Trying to project the date difference between two dates, but I am getting error -
Invalid $project :: caused by :: Unknown expression $dateDiff
db.books.aggregate([{
$project:{
Date_diff:
{$dateDiff:{
start_dt:'$borrowers_list.borrowed_dt',
endDate:'$borrowers_list.return_dt',
unit: "day"
}
}
}
}])
The json document structure is like this -
_id:6188a5283543f7cc2f77c73f
branch_id:1
borrowers_list:Object
0:Object
borrowed_dt:2021-08-15T06:00:00.000+00:00
card_no:"ID000067"
return_dt:2021-08-25T06:00:00.000+00:00
I have no idea why the error is unknown expression $dateDiff, as my syntax is correct. Does anyone have any suggestions?
Based on your provided JSON document, the document should be as below (correct me if it is incorrect):
{
_id: ObjectId("6188a5283543f7cc2f77c73f"),
branch_id: 1,
borrowers_list: {
0: {
borrowed_dt: ISODate("2021-08-15T06:00:00.000+00:00"),
card_no: "ID000067",
return_dt: ISODate("2021-08-25T06:00:00.000+00:00")
}
}
}
]
There is no start_dt in $dateFiff field, it is startDate.
Query
db.collection.aggregate([
{
$project: {
Date_diff: {
$dateDiff: {
startDate: "$borrowers_list.0.borrowed_dt",
endDate: "$borrowers_list.0.return_dt",
unit: "day"
}
}
}
}
])
Note: Above query will perform the $dateDiff for the first document in borrowers_list.
Sample Mongo Playground
In case you need to iterate each document (with key-value pair) in borrowers_list to perform $dateDiff.
$set - Convert from object to array (via $objectToArray) for borrowers_list to new field borrowers.
$set - Iterate each document in borrowers array (1) and perform $dateDiff.
$project - Decorate the output document, convert Date_diff from array to object (via $objectToArray).
Query
db.collection.aggregate([
{
$set: {
borrowers: {
"$objectToArray": "$borrowers_list"
}
}
},
{
$set: {
Date_diff: {
$map: {
input: "$borrowers",
as: "b",
in: {
k: "$$b.k",
v: {
$dateDiff: {
startDate: "$$b.v.borrowed_dt",
endDate: "$$b.v.return_dt",
unit: "day"
}
}
}
}
}
}
},
{
$project: {
Date_diff: {
"$arrayToObject": "$Date_diff"
}
}
}
])
Sample Mongo Playground (Iterate document with key-value pair)

Return null on update mutation GraphQL and Sequelize

I would like to know why my response is "null" when I execute my mutation please.
My query works : the state is_active become true or false in the database SQL when I execute the request.
When I create, I have a correct response, but not on the update.
Graphql request :
mutation {
updateSequence(id: 4, , input: {is_active: true}) {
is_active
}
}
The response :
{
"data": {
"updateSequence": {
"is_active": null
}
}
}
resolvers.js
Mutation : {
createSequence(_, {input}) {
return models.Sequence.create(input);
},
updateSequence(_, {id, input}) {
return models.Sequence.update(input, {where: {id: id}});
}
}
schema.js
# Sequence
type Sequence {
id: Int!,
code: String,
buckets: [Bucket],
sequence_types : [SequenceType]
is_active: Boolean,
}
# SequenceInput
input SequenceInput {
is_active: Boolean
}
...
type Query {
sequences: [Sequence]
sequence(id: Int): Sequence
}
type Mutation {
createSequence(input: SequenceInput): Sequence,
updateSequence(id: ID!, input: SequenceInput): Sequence
}
schema {
query: Query,
mutation: Mutation
}
SOLVED : Migrate MySQL to Postegres to use returning option (only Postegres)
I migrate my database to use the returning option from Sequelize.
createSequence(_, {input}) {
return models.Sequence.create(input);
},
updateSequence(_, {id, input}) {
return models.Sequence.update(input, {where: {id: id}, returning: true}).then((sequence) => {
return sequence[1][0].dataValues;
});
},
According to Sequelize Documentation:
The create method returns a Promise<Model>, in your case it returns something that matches type Sequence.
The update method instead, returns a Promise<Array<number, number>> and that does not match type Sequence.
The promise returns an array with one or two elements. The first element is always the number of affected rows, while the second element is the actual affected rows (only supported in postgres with options.returning true.)
So either you change the return type of updateSequence into something that matches the return type from models.Sequence.update, OR, you have to build an object that matches type Sequence after the update.

GraphQL in Gatsbyjs - Only Return Data Which Contains a Particular Field

I am using GraphQL within the GatsbyJs framework.
I have multiple files with JSON data. The structure of data is similar to this:
{
...,
"sections" / "menuSections"
}
That last field can be either 'sections' or 'menuSections' in each file. My current graphQL query looks like this:
{
allDataJson {
nodes {
menuSections
}
}
}
This query returns the correct 'menuSections', however, data-files which do not have 'menuSections' are returned as null. How can I get GraphQL to only return data from files which contain 'menuSections', i.e. how to return data within which 'menuSections' exist. I am looking for an operator like $exists.
if sections & menuSections are string or arrays of string, maybe you can filter for null:
{
"menuSections": "..."
}
// query
{
allDataJson(filter: {
menuSections: {
ne: null
}
}) {
nodes {
menuSections
}
}
}
If they are object, you can still filter for null, but it has to be applied to one of the field inside that object. If your objects don't have a common field, this won't work:
{
"menuSections": {
"menuSectionField": "..."
}
}
// query
{
allDataJson(filter: {
menuSections: {
menuSectionField: {
ne: null
}
}
}) {
nodes {
menuSections
}
}
}
If they are array of objects, you can do the same thing but with elemMatch:
{
"menuSections": [
{ "menuSectionField": "..." },
{ "other": "..." }
]
}
// query
allDataJson(filter: {
menuSections: {
elemMatch: {
menuSectionField: {
ne: null
}
}
}
}) { ... }
Worst case worst, I think you might be able to define some sort of custom types that ensure existence of menuSections so you can query allDataWithMenuSections etc., but if filter works it's much simpler.
It looks like there isn't an $exists operator in GraphQL. Instead what you can do is add some logic in the resolver to check if a field is not null. I found two older questions related to yours:
GraphQL query and check the returned data
GraphQL query: only include field if not null

Get value of variable from json using jsonslurper

I have the following JSON code:
{
"TIMESTAMP":"2017-05-26-20.22.40.016000",
"dateTime":"2017-05-26H-20.22.4",
"AMUCCY1":"ADP",
"rates":[
{
"AMUCCY2":"AED",
"AMURAT":"1.000000000",
"AMUNXRT":0
},
{
"AMUCCY2":"AFA",
"AMURAT":"1.000000000",
"AMUNXRT":0
},
{
"AMUCCY2":"ALL",
"AMURAT":"1.000000000",
"AMUNXRT":0
},
{
"AMUCCY2":"AMD",
"AMURAT":"1.000000000",
"AMUNXRT":0
}
]
}
Is there quick way in groovy where I could loop through each of the 'rates' and get the value of, let's say 'AMUCCY2' ?
I tried doing this code:
jsonObj.rates.each {
def toCurrencyMap = jsonObj.rates.AMUCCY2
LOG.info "${toCurrencyMap}"
}
but the toCurrencyMap returns an array of all four values of this field. I only want to get each value; not all.
Any suggestions is appreciated.
You can try this:
jsonObj.rates.each {
println it.AMUCCY2
}
If you want list / array:
def result = jsonObj.rates.collect { it.AMUCCY2 }
println result