I want sort a result, but arango ignore my aql-request.
My Query as example:
FOR d IN system_menu
SORT d.Lvl DESC
SORT d.Submenu[*].Lvl DESC
RETURN d
d.Lvl is a INT-Value. How I can sort a array of documents in a document?
my document:
{
"System": {},
"Controller": "reports",
"Show": true,
"Icon": "mdi-newspaper",
"Lvl": 3,
"Title": {
"DEde": "Berichte",
"Universal": "Reports"
},
"Submenu": [
{
"Title": {
"DEde": "Tätigkeitsberichte",
"Universal": "Activity reports"
},
"Controller": "activity-reports",
"Tabmenu": "",
"Filter": "",
"Lvl": 2,
"Show": true,
"Hrule": false
},
{
"Title": {
"DEde": "Behördenbericht",
"Universal": "Authority reports"
},
"Controller": "request-data-subject",
"Tabmenu": "",
"Filter": "",
"Lvl": 1,
"Show": true,
"Hrule": false
},
{
"Title": {
"DEde": "Auskunftsersuchen",
"Universal": "Request from a data subject"
},
"Controller": "request-data-subject",
"Tabmenu": "",
"Filter": "",
"Lvl": 3,
"Show": true,
"Hrule": false
}
]
}
The sorting does not work! How I sort all my documents per INT from 1...100?
To sort an inner array, you can use an inner loop, e.g.
FOR d IN system_menu
SORT d.Lvl DESC
LET submenus = (
FOR s IN d.Submenu
SORT s.Lvl DESC
RETURN s
)
RETURN MERGE(d, { Submenu: submenus })
Related
Repo with example project: https://github.com/Fblfbl/backend-movies
Project on nest.js and typeorm (postgreSQL)
There are 3 entites:
Movies
Users
UserDetails (is movie favorite/watched for user)
UserDetails for movie(and for user) are created only after user add movie to favorite/watched
I want to get all movies for user with userDetails and without it by left join.
It works only when i use this repository method:
async findAllByUser(id: number) {
return this.createQueryBuilder('movie')
.leftJoinAndSelect(
'movie.userDetails',
'userDetails',
`movie.id = userDetails.movieId AND userDetails.userId = ${id}`,
)
.getMany();
}
And there it's example result:
[
{
"id": 1,
"title": "test1",
"genre": "test1",
"userDetails": {
"id": 1,
"isFavorite": false,
"isWatched": true,
"isInWatchlist": false,
"watchingDate": "2021-07-25T19:29:18.510Z",
"userId": 1,
"movieId": 1
}
},
{
"id": 2,
"title": "test2",
"genre": "test2",
"userDetails": {
"id": 2,
"isFavorite": true,
"isWatched": true,
"isInWatchlist": false,
"watchingDate": "2021-07-26T13:08:21.533Z",
"userId": 1,
"movieId": 2
}
},
{
"id": 3,
"title": "test3",
"genre": "test3",
"userDetails": {
"id": 3,
"isFavorite": true,
"isWatched": true,
"isInWatchlist": true,
"watchingDate": "2021-07-26T13:09:11.852Z",
"userId": 1,
"movieId": 3
}
},
{
"id": 4,
"title": "test4",
"genre": "test4",
"userDetails": null
}
]
But if I use one of next two methods instead of the previous one:
async findAllByUser(id: number) {
return this.createQueryBuilder('movie')
.leftJoinAndSelect('movie.userDetails', 'userDetails')
.where(`movie.id = userDetails.movieId`)
.andWhere('userDetails.userId = :userId', { userId: id })
.getMany();
}
async findAllByUser(id: number) {
return this.find({
relations: ['userDetails'],
join: {
alias: 'movie',
leftJoinAndSelect: { userDetails: 'movie.userDetails' },
},
where: (qb) => {
qb.where('userDetails.userId = :userId', { userId: id });
},
});
}
I get this (no movie with id 4 which haven't userDetails for user):
[
{
"id": 1,
"title": "test1",
"genre": "test1",
"userDetails": {
"id": 1,
"isFavorite": false,
"isWatched": true,
"isInWatchlist": false,
"watchingDate": "2021-07-25T19:29:18.510Z",
"userId": 1,
"movieId": 1
}
},
{
"id": 2,
"title": "test2",
"genre": "test2",
"userDetails": {
"id": 2,
"isFavorite": true,
"isWatched": true,
"isInWatchlist": false,
"watchingDate": "2021-07-26T13:08:21.533Z",
"userId": 1,
"movieId": 2
}
},
{
"id": 3,
"title": "test3",
"genre": "test3",
"userDetails": {
"id": 3,
"isFavorite": true,
"isWatched": true,
"isInWatchlist": true,
"watchingDate": "2021-07-26T13:09:11.852Z",
"userId": 1,
"movieId": 3
}
}
]
All methods use left join, but why the result is different? Can you explain please
I'm just seeing this and thinking it might be a little too late, but might as well answer for the next one that comes around. The differences between the two queries lie on which part of the expression. The first one is something like:
"Find me all the movies in the movies table, and, from each movie, gather up the related userDetails that belong to the user with ID 1"
Note the condition being directly tied to the userDetail, and not the movie selection. Whereas the second one:
"Find me all movies and all of their related userDetails, where the userId belonging to the userDetail equals 1
Now you can think of the movie + its userDetails being bundled up into one row of results, and then filtering over each row, keeping only those that have at least one userDetail whose userId equals 1.
I hope this clarifies this issue.
I have this json schema
"header": {
"self": {},
"items": [
{
"_id": "5ec7e61979ec9914ecefc539",
"title": "Test",
"root": "true",
"alignment": "left",
"page": "test",
"translate": "",
"toggle": "",
"icon": "",
"IsActive": 1,
"submenu": [
{
"_id": "5ece913a353a71309084768d",
"title": "Sub Test",
"bullet": "dot",
"page": "test",
"translate": "MENU.TEST1",
"icon": "flaticon-stes-3",
"IsActive": 1
},
{
"_id": "5ece935d79972f0390997179",
"title": "Sub Test",
"bullet": "dot",
"page": "test",
"translate": "MENU.TEST2",
"icon": "flaticon-stes-3",
"IsActive": 1
}
]
}
]
}
// Index based on a previous query
this.db.collection('AssetData').find({"header.items.$.submenu.[0]._id":ObjectID("5ece913a353a71309084768d"));
//Tried with elemMatch
this.db.collection('AssetData').find(
{
"header.items": {
$elemMatch:{
"submenu": {
$elemMatch:{
"_id":ObjectID("5ece913a353a71309084768d")
}
}
}
}
});
And I am wanting to retrieve one of the sub-menu object data based on the _id from the sub-menu, but I'm having trouble retrieving it.
I'm not sure If I could use an index of the second array from another query to obtain the data, or if there's another way that I'm missing like elem match.
I am using MongoDB 3.5.6.
What would be the best way to retrieve this?
// Index based on a previous query
this.db.collection('AssetData').find({"header.items.$.submenu.[0]._id":ObjectID("5ece913a353a71309084768d")});
//Tried with elemMatch
this.db.collection('AssetData').find(
{
"header.items": {
$elemMatch:{
"submenu": {
$elemMatch:{
"_id":ObjectID("5ece913a353a71309084768d")
}
}
}
}
}).exec(function(err, item) {
console.log(item);
// here you can retrieve the stock from item, as you wish
});
hopefully it will help you
I'm trying to do a kind of a complex mongoose query. I have searched around and found solutions asking for single variables(with if) and building the query object, but I wanted to know if there is a simpler way because I would be building a query with about 40 non required parameters, for example right now in my req.body I have:
{ searchParams:
{ publicationType: 'Venta',
subtype: [ 'Duplex', 'Cabaña' ],
address:
{
streetNumber: '1312',
streetName: 'José María Bosch',
city: 'Villa Bosch',
state: 'Buenos Aires',
country: 'Argentina' }
}
}
All the names are the same in the database, what I have to do I search if publicationType matches, if the address matches and finally for sub type if any of those array items matches since subtype is a string. Is there any way to do it without an if for each field?
Right now I did this simple query to try out
router.post('/search', (req, res, next) => {
model.Dwelling.find(req.body.searchParams).lean().exec().then(
dwellings => res.send({dwellings})
).catch(next);
});
It works on publicationType and subtype buy not on address.
this is an object in my data base:
{
"_id": {
"$oid": "5af06167ea174f00142bab91"
},
"publicationType": "Alquiler",
"address": {
"latitude": -34.59250669999999,
"altitude": -58.571258599999965,
"streetNumber": "1312",
"streetName": "José María Bosch",
"city": "Villa Bosch",
"state": "Buenos Aires",
"country": "Argentina"
},
"type": "Residencial",
"subtype": "Casa",
"currency": "",
"price": 50000,
"occupationStatus": "Disponible",
"spaces": {
"rooms": 3,
"floors": 0,
"bedrooms": 0,
"closets": 0,
"bathRoom": 0,
"toilette": 0,
"living": false,
"livingDining": false,
"diningRoom": false,
"kitchen": false,
"kitchenDining": false,
"terrace": false,
"balcony": false,
"backYard": false,
"swimmingPool": false,
"barbecue": false,
"garage": "No",
"laundryRoom": "No"
},
"features": {
"status": "Desconocido",
"orientation": "Desconocida",
"luminosity": "Desconocida",
"heating": [
{
"value": "No posee",
"label": "No posee"
}
],
"refurbished": false,
"repair": "No"
},
"services": {
"gas": true,
"water": true,
"sewer": true,
"phone": true,
"pavement": true,
"electricity": true,
"cableTv": true
},
"legal": {
"bank": false,
"prof": false
},
"generalDescription": "Desc 1",
"privateDescription": "Desc 2",
"siocId": 713675,
"createdAt": {
"$date": "2018-05-07T14:23:35.230Z"
},
"updatedAt": {
"$date": "2018-05-07T14:23:35.230Z"
},
"__v": 0
}
I need to write a Select query for the below JSON data in Azure DatabaseDB.
{
"Result": [
{
"media": [
{
"url": "https://someurl.com",
"thumb_url": "https://someurl.com",
"id": "f545f874-a9b4-4573-a0b0-b2d50a7994e0",
"removed": false,
"size": 133454,
"length": 0,
"type": "IMG",
"avail": true,
"has_thumb": true,
"tagged_chi": [
{
"chi_id": "1069b9ef-1028-45f4-b9a1-a40e0d438f4e",
"tag_x": 262.048,
"tag_y": 157.472,
"tag_by": "d481a522-6e2f-4dc6-8aeb-bc87cf27287d",
"created": 1486723018,
"last_updated": 1486723018
},
{
"chi_id": "7102fc10-62e8-4d0a-9fcf-35645253fcef",
"tag_x": 231.648,
"tag_y": 146.528,
"tag_by": "d481a522-6e2f-4dc6-8aeb-bc87cf27287d",
"created": 1486723018,
"last_updated": 1486723018
}
],
"created": 1486723012,
"last_updated": 1486723017
}
],
"id": "23bcd070-0f64-4914-8bc1-d5e936552295",
"acc_id": "d481a522-6e2f-4dc6-8aeb-bc87cf27287d",
"chi_id": "7102fc10-62e8-4d0a-9fcf-35645253fcef",
"is_note": false,
"title": "",
"when": -2147483648,
"loc_id": null,
"col_id": null,
"comment": null,
"removed": false,
"created": -2147483648,
"last_updated": -2147483648,
"note_type": null,
"note_value": null
},
{
"media": [
{
"url": "https://someurl.com",
"thumb_url": "https://someurl.com",
"id": "7665b921-2790-496b-a70f-30afae43d8c6",
"removed": false,
"size": 6872977,
"length": 0,
"type": "IMG",
"avail": true,
"has_thumb": true,
"tagged_chi": [
{
"chi_id": "1069b9ef-1028-45f4-b9a1-a40e0d438f4e",
"tag_x": 2305.152,
"tag_y": 686.5653,
"tag_by": "d481a522-6e2f-4dc6-8aeb-bc87cf27287d",
"created": 1486976119,
"last_updated": 1486976119
},
{
"chi_id": "7102fc10-62e8-4d0a-9fcf-35645253fcef",
"tag_x": 1070.757,
"tag_y": 1038.741,
"tag_by": "d481a522-6e2f-4dc6-8aeb-bc87cf27287d",
"created": 1486976119,
"last_updated": 1486976119
}
],
"created": 1486976100,
"last_updated": 1486976118
}
],
"id": "58fa3c58-5508-4371-83f4-405332c636e1",
"acc_id": "d481a522-6e2f-4dc6-8aeb-bc87cf27287d",
"chi_id": "7102fc10-62e8-4d0a-9fcf-35645253fcef",
"is_note": false,
"title": "",
"when": -2147483648,
"loc_id": null,
"col_id": null,
"comment": null,
"removed": false,
"created": -2147483648,
"last_updated": -2147483648,
"note_type": null,
"note_value": null
}
],
"Continuation": null
}
I was trying something like below but it is not working for me. I want the data matched to Media => tagged_chil => id
Query suggested by, #peter-tirrell:
string.Format("select c.id, c.acc_id, c.chi_id, c.is_note, c.title, c.loc_id, c.media, t from c JOIN m IN c.media JOIN t IN m.tagged_chi where c.chi_id = '{0}' OR t.chi_id = '{0}'", childId)
Minor changes in #peter-tirrell's query:
string.Format("select c.id, c.acc_id, c.chi_id, c.is_note, c.title, c.loc_id, c.media, t from c JOIN m IN c.media JOIN t IN m.tagged_chi where c.chi_id = '{0}' OR ( t.chi_id != c.chi_id AND t.chi_id = '{0}')", childId)
I am getting duplicate records if the c.child and t.child both are having same values.
You could potentially use JOINs to flatten the structure which might help with querying, too. Something like:
select
c.id,
c.acc_id,
c.chi_id,
c.is_note,
c.title,
c.loc_id,
m,
t
from c JOIN m IN c.media
JOIN t IN m.tagged_chi
where c.chi_id = {0} OR t.id = {0}
Then you can select out whichever specific data fields you need.
Base on my experience, your query code will return null. Because ARRAY_CONTAINS it will return a Boolean indicating whether the array contains the specified value. That means your query code can be short as SELECT * FROM TimelineEvent t WHERE OR ARRAY_CONTAINS ( t.media, true) that will return null in your case.
Please have a try to use the following code:
SELECT * FROM TimelineEvent t WHERE ARRAY_CONTAINS ( t.media[0].tagged_chi, {
"id":"0af23202-07f9-40a0-90ba-d2e2f6679331"
})
We also could use UDFs to implement it with customized code, more detail about UDF,please refer to document.
I have large amount of rows on a csv file, which look like:
name a,1
name b,1
name c,1
name d,2
name e,2
I need to concatenate the rows based on number. Result should be:
name a|name b|name c
name d|name e
How can I do it in Google Refine or in Excel/Google Spreadsheet?
I am thinking of it, but with no solution.
Thank you a lot!
Here is a proposal with Open refine. The only Grel formula i used is:
row.record.cells['myColumn'].value.join('|')
screencast
And here is the JSOn, assuming that your first column is named "myColumn" and the second "number" :
[
{
"op": "core/column-addition",
"description": "Create column test at index 2 based on column number using expression grel:value",
"engineConfig": {
"mode": "row-based",
"facets": [
{
"omitError": false,
"expression": "isBlank(value)",
"selectBlank": false,
"selection": [
{
"v": {
"v": false,
"l": "false"
}
}
],
"selectError": false,
"invert": false,
"name": "ee",
"omitBlank": false,
"type": "list",
"columnName": "ee"
}
]
},
"newColumnName": "test",
"columnInsertIndex": 2,
"baseColumnName": "number",
"expression": "grel:value",
"onError": "set-to-blank"
},
{
"op": "core/column-move",
"description": "Move column test to position 0",
"columnName": "test",
"index": 0
},
{
"op": "core/blank-down",
"description": "Blank down cells in column test",
"engineConfig": {
"mode": "row-based",
"facets": [
{
"omitError": false,
"expression": "isBlank(value)",
"selectBlank": false,
"selection": [
{
"v": {
"v": false,
"l": "false"
}
}
],
"selectError": false,
"invert": false,
"name": "ee",
"omitBlank": false,
"type": "list",
"columnName": "ee"
}
]
},
"columnName": "test"
},
{
"op": "core/column-addition",
"description": "Create column concatenation at index 2 based on column myColumn using expression grel:row.record.cells['myColumn'].value.join('|')",
"engineConfig": {
"mode": "row-based",
"facets": [
{
"omitError": false,
"expression": "isBlank(value)",
"selectBlank": false,
"selection": [
{
"v": {
"v": false,
"l": "false"
}
}
],
"selectError": false,
"invert": false,
"name": "ee",
"omitBlank": false,
"type": "list",
"columnName": "ee"
}
]
},
"newColumnName": "concatenation",
"columnInsertIndex": 2,
"baseColumnName": "myColumn",
"expression": "grel:row.record.cells['myColumn'].value.join('|')",
"onError": "set-to-blank"
}
]
If you can use Python it would be pretty easy to do this manipulation. In the the code below, the name and group are read from "input.csv", and the grouped names (along with the group) are written to "output.csv". A defaultdict is used to create empty lists to store the group members.
import collections
import csv
grouped = collections.defaultdict(list)
with open('input.csv') as fp:
reader = csv.reader(fp)
for row in reader:
name, group = row
grouped[group].append(name)
with open('output.csv', 'w', newline='') as fp:
writer = csv.writer(fp, delimiter='|')
for key in sorted(grouped.keys()):
writer.writerow([key] + grouped[key])