Grouping select with partial - postgresql

Hello i have here this code:
public function getConversations($user, $limit, $page)
{
$offset = ($page - 1) * $limit;
$qb = $this->createQueryBuilder('message');
$qb
->select('message, partial o.{id}, partial userFrom.{id, username, avatar}, partial userTo.{id, username, avatar}, partial availability.{id, timeFrom}')
->leftJoin('message.from', 'userFrom')
->leftJoin('message.to', 'userTo')
->leftJoin('message.order', 'o')
->leftJoin('o.availabilities', 'availability')
->where($qb->expr()->orX(
$qb->expr()->eq('message.from', ':user'),
$qb->expr()->eq('message.to', ':user')
))
->setParameter('user', $user)
->setFirstResult($offset)
->setMaxResults($limit)
->addOrderBy('message.created', Criteria::DESC);
return $qb->getQuery()->getArrayResult();
}
I want to get only last message connected to each order. If i have 2 messages to order it gives me 2 rows but i need only one (the last one). I tried to use GROUP BY but it didn't work well. It wanted me to put all foreign ids to group by and then the result was really bad.
Result for this is:
{
"id": "813fa986-f89b-411d-8324-727e427cbc01",
"isRead": false,
"created": {
"date": "2016-12-12 18:01:00.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"content": "Hello to you too",
"contentType": "text",
"from": {
"id": "3e1806d6-f47e-48b4-afdb-1081d9555a3f",
"avatar": "",
"username": "User two"
},
"to": {
"id": "52ead9fa-b498-400b-967d-b49886ab3118",
"avatar": "",
"username": "User one"
},
"order": {
"id": "8349e718-405a-4e88-8155-3896e9a89fdf",
"availabilities": [
{
"id": "9ddc07e0-be3d-453c-b12f-0ee31bfce444",
"timeFrom": {
"date": "2016-12-03 12:00:00.000000",
"timezone_type": 3,
"timezone": "UTC"
}
}
]
}
},
{
"id": "813fa986-f89b-411d-8324-727e427cbc00",
"isRead": false,
"created": {
"date": "2016-12-12 18:00:00.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"content": "Helllooooooo",
"contentType": "text",
"from": {
"id": "52ead9fa-b498-400b-967d-b49886ab3118",
"avatar": "",
"username": "User one"
},
"to": {
"id": "3e1806d6-f47e-48b4-afdb-1081d9555a3f",
"avatar": "",
"username": "User two"
},
"order": {
"id": "8349e718-405a-4e88-8155-3896e9a89fdf",
"availabilities": [
{
"id": "9ddc07e0-be3d-453c-b12f-0ee31bfce444",
"timeFrom": {
"date": "2016-12-03 12:00:00.000000",
"timezone_type": 3,
"timezone": "UTC"
}
}
]
}
and should be just like this:
{
"id": "813fa986-f89b-411d-8324-727e427cbc01",
"isRead": false,
"created": {
"date": "2016-12-12 18:01:00.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"content": "Hello to you too",
"contentType": "text",
"from": {
"id": "3e1806d6-f47e-48b4-afdb-1081d9555a3f",
"avatar": "",
"username": "User two"
},
"to": {
"id": "52ead9fa-b498-400b-967d-b49886ab3118",
"avatar": "",
"username": "User one"
},
"order": {
"id": "8349e718-405a-4e88-8155-3896e9a89fdf",
"availabilities": [
{
"id": "9ddc07e0-be3d-453c-b12f-0ee31bfce444",
"timeFrom": {
"date": "2016-12-03 12:00:00.000000",
"timezone_type": 3,
"timezone": "UTC"
}
}
]
}
}

Related

Pymongo "and" operator not filtering query results [duplicate]

This question already has answers here:
Retrieve only the queried element in an object array in MongoDB collection
(18 answers)
Closed 25 days ago.
I have this monogdb schema:
groups = {
"bsonType": "object",
"required": [
"created_at", "group_name", "owner",
"members", "currency", "country",
"group_username"
],
"properties": {
"created_at": {
"bsonType": "date",
"description": "The date and time when the group was created"
},
"group_name": {
"bsonType": "string",
"description": "Name of group"
},
"group_username": {
"bsonType": "string",
"description": "groups username"
},
"country": {
"bsonType": "string",
"description": "User's country and is required"
},
"currency": {
"bsonType": "string",
"description": "User's currency type and is required"
},
"description": {
"bsonType": "string",
"description": "Brief description of what the group does"
},
"block": {
"bsonType": "bool",
"description": "Field to check if user disabled from group activity"
},
"owner": {
"bsonType": "object",
"required": ["user_id", "status"],
"properties": {
"user_id": {
"bsonType": "objectId",
"description": "User id of the owner of the group"
},
"status": {
"bsonType": "string",
"description": "Default value is 'owner'",
}
},
},
"group_invite_link": {
"bsonType": "string",
"description": "Link used to invite new members"
},
"members": {
"bsonType": "array",
"items": {
"bsonType": "object",
"required": ["user_id", "name", "status", "confirmed", "joined_at"],
"properties": {
"user_id": {
"bsonType": "objectId"
},
"name": {
"bsonType": "string",
"description": "Name of group member"
},
"status": {
"enum": ["admin", "member"],
"description": "can only be one of the enum values and is required"
},
"confirmed": {
"bsonType": "bool",
"description": "Confirms if user is fully authorized to operate in group"
},
"joined_at": {
"bsonType": "date",
"description": "Date user joined group"
}
}
}
},
"group_wallet_id": {
"bsonType": "objectId",
"description": "Id of wallet associated with group"
}
}
}
and I am querying it to return only documents that match the below query:
db.groups.find({
"$and": [
{
"members.user_id": ObjectId(user_id)
},
{
"members.confirmed": True
}
]
})
It returns this:
{
"sucess": true,
"message": "Users groups",
"data": [
{
"_id": "63987c4263d7eba1e79a1df1",
"created_at": "2022-12-13 13:21:03",
"group_name": "Demo 1",
"is_owner": true,
"balance": {
"$numberDecimal": "250.00"
},
"members": [
{
"user_id": "63987b7c63d7eba1e79a1dd7",
"name": "Joy Kudosen",
"status": "admin",
"confirmed": true,
"joined_at": "2022-12-13 13:21:03"
},
{
"user_id": "63987b7c63d7eba1e79a1dd8",
"name": "Joy Kudosen",
"status": "member",
"confirmed": true,
"joined_at": "2022-12-14 13:22:03"
},
{
"user_id": "63987b7c63d7eba1e79a1dd7",
"name": "Joy Kudosen",
"status": "admin",
"confirmed": true,
"joined_at": "2022-12-14 11:05:39"
}
]
},
{
"_id": "63987c5a63d7eba1e79a1df4",
"created_at": "2022-12-13 13:21:27",
"group_name": "Demo 2",
"is_owner": true,
"balance": {
"$numberDecimal": "100"
},
"members": [
{
"user_id": "63987b7c63d7eba1e79a1dd7",
"name": "Joy Kudosen",
"status": "admin",
"confirmed": true,
"joined_at": "2022-12-13 13:21:27"
}
]
},
{
"_id": "6399ae06ebaca8cd5fbcd639",
"created_at": "2022-12-14 11:05:39",
"group_name": "Demo 4",
"is_owner": true,
"balance": {
"$numberDecimal": "0"
},
"members": [
{
"user_id": "63987b7c63d7eba1e79a1dd7",
"name": "Joy Kudosen",
"status": "admin",
"confirmed": true,
"joined_at": "2022-12-14 11:05:39"
}
]
},
{
"_id": "63d1810699edcb7379f21c27",
"created_at": "2023-01-25 19:20:33",
"group_name": "Hello world",
"is_owner": false,
"balance": {
"$numberDecimal": "0"
},
"members": [
{
"user_id": "63d180c399edcb7379f21c23",
"name": "Joy Kudosen",
"status": "admin",
"confirmed": true,
"joined_at": "2023-01-25 19:20:33"
},
{
"user_id": "63987b7c63d7eba1e79a1dd7",
"name": "Joy Kudosen",
"status": "member",
"confirmed": false,
"joined_at": "2023-01-25 19:23:31"
}
]
}
]
}
I want to return only array objects that match the user's id and "confirmed" value of "true" in the members array like this:
{
"sucess": true,
"message": "Users groups",
"data": [
{
"_id": "63987c4263d7eba1e79a1df1",
"created_at": "2022-12-13 13:21:03",
"group_name": "Demo 1",
"is_owner": true,
"balance": {
"$numberDecimal": "250.00"
},
"members": [
{
"user_id": "63987b7c63d7eba1e79a1dd7",
"name": "Joy Kudosen",
"status": "admin",
"confirmed": true,
"joined_at": "2022-12-13 13:21:03"
},
{
"user_id": "63987b7c63d7eba1e79a1dd8",
"name": "Joy Kudosen",
"status": "member",
"confirmed": true,
"joined_at": "2022-12-14 13:22:03"
},
{
"user_id": "63987b7c63d7eba1e79a1dd7",
"name": "Joy Kudosen",
"status": "admin",
"confirmed": true,
"joined_at": "2022-12-14 11:05:39"
}
]
},
{
"_id": "63987c5a63d7eba1e79a1df4",
"created_at": "2022-12-13 13:21:27",
"group_name": "Demo 2",
"is_owner": true,
"balance": {
"$numberDecimal": "100"
},
"members": [
{
"user_id": "63987b7c63d7eba1e79a1dd7",
"name": "Joy Kudosen",
"status": "admin",
"confirmed": true,
"joined_at": "2022-12-13 13:21:27"
}
]
},
{
"_id": "6399ae06ebaca8cd5fbcd639",
"created_at": "2022-12-14 11:05:39",
"group_name": "Demo 4",
"is_owner": true,
"balance": {
"$numberDecimal": "0"
},
"members": [
{
"user_id": "63987b7c63d7eba1e79a1dd7",
"name": "Joy Kudosen",
"status": "admin",
"confirmed": true,
"joined_at": "2022-12-14 11:05:39"
}
]
}
]
}
I have used different forms of this query but I am unable to filter out the "hello world" group information. I have tried aggregation and the "or" operation and still get the same results.
"$elemMatch" is used to find documents that match multiple fields within an array element.
For example, like this:
db.groups.find({
"members": {
"$elemMatch": {
"user_id": "63987b7c63d7eba1e79a1dd7",
"confirmed": true
}
}
})
Try it on mongoplayground.net.

how to update the first object in the array of quarterFinals that matches some mongoDB condition?

I have the document below and I need to iterate through the quarterFinals array and update the first null address property I find to '123'
{
"data": {
"createTournament": {
"_id": "613122d0c7befdeef3b0b571",
"title": "Jogo de truco",
"description": "",
"location": "Batalha na rua",
"type": "Battle",
"players": [],
"status": "PENDING",
"size": 8,
"entryFee": 1,
"prizePool": 20,
"currency": "USD",
"startDate": "2021-09-01",
"endDate": "2021-09-01",
"rounds": {
"quarterFinals": [{
"id": "9fb6f9ca-c06a-4972-b140-0f2425a90707",
"arena": "",
"firstParticipant": {
"address": null,
"battlesWon": 0
},
"secondParticipant": {
"address": null,
"battlesWon": 0
}
}]
}
}
}
}
One way of doing this is
exports.updateData = async (tournamentID,address)=>{
return await db_collection.updateOne({id:tournamentId,rounds:{$elemMatch:{quarterfinals.address:null}}},{$push:{address:address});
};

Retrieve only matched object from nested array in mongodb

In this json , I need a find query which finds all the field where the "status":"Y", if the parent field has "status":"N", ignore the child field , else find the child field where the "status":"Y" along with its parent field
Note: The sub field is in a array
[
{
"type": "Type 1",
"status": "Y",
"code": "1",
"category": [
{
"type": "Cat 1",
"status": "Y",
"code": "1000",
"subcategories": [
{
"type": "Sub 1",
"status": "N",
"code": "1001"
},
{
"type": "Sub 2",
"status": "N",
"code": "1002"
},
{
"type": "Sub 3",
"status": "Y",
"code": "1003"
}
]
},
{
"type": "Cat 2",
"status": "N",
"code": "2000",
"subcategories": [
{
"type": "Sub 4",
"status": "Y",
"code": "2001"
},
{
"type": "Sub 5",
"status": "Y",
"code": "2002"
}
]
}
]
}
]
My Output Should be like this
[
{
"type": "Type 1",
"status": "Y",
"code": "1",
"category": [
{
"type": "Cat 1",
"status": "Y",
"code": "1000",
"subcategories": [
{
"type": "Sub 3",
"status": "Y",
"code": "1003"
}
]
} ]
}
]
Thanks in Advance:)
You can try below aggregation
db.collection.aggregate([
{ "$match": { "status": "Y" }},
{ "$unwind": "$category" },
{ "$match": { "category.status": "Y" } },
{ "$project": { "type": 1, "status": 1, "code": 1,
"category.type": "$category.type",
"category.status": "$category.status",
"category.code": "$category.code",
"category.subcategories": {
"$filter": {
"input": "$category.subcategories",
"as": "subcategory",
"cond": {
"$eq": [
"$$subcategory.status",
"Y"
]
}
}
}
}},
{ "$group": {
"_id": "$_id",
"type": { "$first": "$type" },
"status": { "$first": "$status" },
"code": { "$first": "$code" },
"category": { "$push": "$category" }
}}
]).then((data) => {
res.send(data)
})
Gives you following output (check here)
[
{
"_id": ObjectId("5a934e000102030405000000"),
"category": [
{
"code": "1000",
"status": "Y",
"subcategories": [
{
"code": "1003",
"status": "Y",
"type": "Sub 3"
}
],
"type": "Cat 1"
}
],
"code": "1",
"status": "Y",
"type": "Type 1"
}
]

what is the date format from stash api?

In the below json response, what is the date format for createdDate and updatedDate? I am not sure how to work in reverse to find what format the api is using for date. I couldn't find this any where in the documentation.
{
"size": 1,
"limit": 25,
"isLastPage": true,
"values": [
{
"id": 101,
"version": 1,
"title": "Talking Nerdy",
"description": "It’s a kludge, but put the tuple from the database in the cache.",
"state": "OPEN",
"open": true,
"closed": false,
"createdDate": 1359075920,
"updatedDate": 1359085920,
"fromRef": {
"id": "refs/heads/feature-ABC-123",
"repository": {
"slug": "my-repo",
"name": null,
"project": {
"key": "PRJ"
}
}
},
"toRef": {
"id": "refs/heads/master",
"repository": {
"slug": "my-repo",
"name": null,
"project": {
"key": "PRJ"
}
}
},
"locked": false,
"author": {
"user": {
"name": "tom",
"emailAddress": "tom#example.com",
"id": 115026,
"displayName": "Tom",
"active": true,
"slug": "tom",
"type": "NORMAL"
},
"role": "AUTHOR",
"approved": true
},
"reviewers": [
{
"user": {
"name": "jcitizen",
"emailAddress": "jane#example.com",
"id": 101,
"displayName": "Jane Citizen",
"active": true,
"slug": "jcitizen",
"type": "NORMAL"
},
"role": "REVIEWER",
"approved": true
}
],
"participants": [
{
"user": {
"name": "dick",
"emailAddress": "dick#example.com",
"id": 3083181,
"displayName": "Dick",
"active": true,
"slug": "dick",
"type": "NORMAL"
},
"role": "PARTICIPANT",
"approved": false
},
{
"user": {
"name": "harry",
"emailAddress": "harry#example.com",
"id": 99049120,
"displayName": "Harry",
"active": true,
"slug": "harry",
"type": "NORMAL"
},
"role": "PARTICIPANT",
"approved": true
}
],
"link": {
"url": "http://link/to/pullrequest",
"rel": "self"
},
"links": {
"self": [
{
"href": "http://link/to/pullrequest"
}
]
}
}
],
"start": 0
}
Just making a note that in my case, it is a UNIX timestamp, but I have to remove three trailing zeroes. E.g. the data looks like this:
"createdDate":1555621993000
If interpreted as a UNIX timestamp, that would be 09/12/51265 # 4:16am (UTC).
By removing the three trailing zeroes I get 1555621993, which is the correct time 04/18/2019 # 9:13pm (UTC)
Your mileage may vary but that was a key discovery for me :)
It looks like a UNIX timestamp.
https://en.wikipedia.org/wiki/Unix_time

Domino 9.x calendar service create meeting

I have been following this guide to work on Domino 9.0.1
Domino Calendar services
I am using JSON and the POST command works but creates an appointment, what I want to do is create a meeting. I have tried setting other fields like event['x-lotus-appttype'].data or event.AppointmentType = 3 but I still get an appointment.
JSON I am sending
{
"events": [
{
"summary":"Meeting 1",
"location":"Location 1",
"start": {
"date":"2013-12-01",
"time":"13:00:00",
"utc":true
},
"end": {
"date":"2013-12-01",
"time":"14:00:00",
"utc":true
}
}
]
}
What is the correct JSON format to create a meeting ?
Take a look at the following documentation: Event with attendees represented in JSON format
EXAMPLE 4. Event with attendees and time zone array:
{
"x-lotus-charset": {
"data": "UTF-8"
},
"timezones": [
{
"tzid": "Eastern",
"standard": {
"start": {
"date": "1950-11-05",
"time": "02:00:00"
},
"offsetFrom": "-0400",
"offsetTo": "-0500",
"recurrenceRule": "FREQ=YEARLY;BYMONTH=11;BYDAY=1SU;BYHOUR=2;BYMINUTE=0"
},
"daylight": {
"start": {
"date": "1950-03-12",
"time": "02:00:00"
},
"offsetFrom": "-0500",
"offsetTo": "-0400",
"recurrenceRule": "FREQ=YEARLY;BYMONTH=3;BYDAY=2SU;BYHOUR=2;BYMINUTE=0"
}
}
],
"events": [
{
"href": "/mail/dlawson.nsf/api/calendar/events/DB7E0BAC21EC322A85257BD200756E26-Lotus_Notes_Generated",
"id": "DB7E0BAC21EC322A85257BD200756E26-Lotus_Notes_Generated",
"summary": "Staff meeting",
"location": "Ray's office",
"description": "Please email your status update 24 hours before the meeting.",
"start": {
"date": "2013-09-12",
"time": "09:00:00",
"tzid": "Eastern"
},
"end": {
"date": "2013-09-12",
"time": "10:00:00",
"tzid": "Eastern"
},
"class": "public",
"transparency": "opaque",
"sequence": 0,
"last-modified": "20130825T212457Z",
"attendees": [
{
"role": "chair",
"status": "accepted",
"rsvp": false,
"displayName": "Duke Lawson/Peaks",
"email": "DukeLawson#swg.usma.ibm.com"
},
{
"role": "req-participant",
"status": "needs-action",
"rsvp": true,
"displayName": "Dean Melnyk/Peaks",
"email": "DeanMelnyk#swg.usma.ibm.com"
},
{
"role": "req-participant",
"status": "needs-action",
"rsvp": true,
"displayName": "Raymond Chan/Peaks",
"email": "RaymondChan#swg.usma.ibm.com"
}
],
"organizer": {
"displayName": "Duke Lawson/Peaks",
"email": "DukeLawson#swg.usma.ibm.com"
},
"x-lotus-broadcast": {
"data": "FALSE"
},
"x-lotus-notesversion": {
"data": "2"
},
"x-lotus-appttype": {
"data": "3"
}
}
]
}
I hope this can help :)