MongoDB sorting nested array of objects by object property value - node.js

Hello i am new in MongoDB and NodeJS. How can i sort this array of objects for example by ELO, GAMES_COUNT, TIME, users firstname etc. with MongoDB mongoose aggregate ?
Here is an examle:
"statistics": [
{"statistics": [
{
"type": "ELO",
"value": 1022
},
{
"type": "GAMES_COUNT",
"value": 5
},
{
"type": "TIME",
"value": 3112
},
],
"user": {
"_id": "1",
"firstName": "Pepa",
"lastName": "Novák",
}
},
{
"statistics": [
{
"type": "ELO",
"value": 1028
},
{
"type": "GAMES_COUNT",
"value": 4
},
{
"type": "TIME",
"value": 2151
},
],
"user": {
"_id": "2",
"firstName": "Václav",
"lastName": "Rupinsky"
}
}
]

Related

How to set up aggregation pipeline for specific use case in MongoDB

`I have two collections in MongoDB. One is users and the other is companies. I need to be able to populate the users.endpoints with the endpoints from companies that match ObjectID's which are in the user.endPoints property. It is a bit hard for me to wrap my head around considering there are two steps.
This is the companies collection.
{
"_id": {
"$oid": "63ed39162bfc2cf8065b76cf"
{
"_id": {
"$oid": "63ed39162bfc2cf8065b76cf"
},
"companyName": "Sowegatel",
"amount": 0,
"signalwireSid": "id",
"numbers": [
{
"number": "+12068133580",
"_id": {
"$oid": "63ed39c72bfc2cf8065b76f0"
},
"createdAt": {
"$date": {
"$numberLong": "1676491207931"
}
},
"updatedAt": {
"$date": {
"$numberLong": "1676491207931"
}
}
}
],
"endPoints": [
{
"userName": "4009",
"_id": "fb1d0ef9-c713-400e-a3c9-cbc6823aaf57"
},
{
"userName": "4019",
"_id": "506e710d-14a6-4345-bc89-8488af4cabe4"
},
{
"userName": "4020",
"_id": "c80bd1ab-ca8d-4649-9d35-56d64ef8fab5",
"type": "sip_endpoint"
}
],
"__v": 6
} },
"companyName": "Sowegatel",
"amount": 0,
"signalwireSid": "id",
"numbers": [
{
"number": "+12068133580",
"_id": {
"$oid": "63ed39c72bfc2cf8065b76f0"
},
"createdAt": {
"$date": {
"$numberLong": "1676491207931"
}
},
"updatedAt": {
"$date": {
"$numberLong": "1676491207931"
}
}
}
],
"endPoints": [
{
"userName": "4009",
"_id": "fb1d0ef9-c713-400e-a3c9-cbc6823aaf57"
},
{
"userName": "4019",
"_id": "506e710d-14a6-4345-bc89-8488af4cabe4"
},
{
"userName": "4020",
"_id": "c80bd1ab-ca8d-4649-9d35-56d64ef8fab5",
"type": "sip_endpoint"
}
],
"__v": 6
}
This is the users collection.
{
"_id": {
"$oid": "63ed39162bfc2cf8065b76d2"
},
"firstName": "mark",
"lastName": "thomas",
"email": "mt#st.com",
"password": "",
"companyId": {
"$oid": "63ed39162bfc2cf8065b76cf"
},
"active": true,
"role": "companyAdmin",
"tokens": [],
"__v": 2,
"endpoints": [
// I need to populate to endpoints here!!!! //
"c80bd1ab-ca8d-4649-9d35-56d64ef8fab5"
]
}
I can do a single step aggregation, but this is a little complicated for me.`

Typescript to update Mongodb

Got thrown into a Typescript/Mongodb project and am having some trouble getting started. I've been asked to convert data from
{
"_id": {
"$oid": "636c0a8dce0eb36dc13e63b3"
},
"status": "active",
"companies": [
{
"$oid": "5f64bd6785fad3e83771bb4f"
}
],
"whitelists": [],
"taxonomies": [
{
"$oid": "6080538565c2036dbeadcdfa"
},
{
"$oid": "6080538765c2036dbeadce07"
},
{
"$oid": "6080538d65c2036dbeadce44"
},
{
"$oid": "6080538d65c2036dbeadce46"
}
],
"itemType": "unit",
"attributes": [
{
"_id": {
"$oid": "6384d2bdf29f40634fe77db4"
},
"type": "ingredients:statement",
"key": "0",
"parent": "nutrition:panel#0",
"uid": "ingredients:statement#0",
"value": {
"footnotes": [],
"statement": "ingredients: water, butter, lemon juice, wheat flour, egg yolks, sea salt, xanthan gum, turmeric, carotenes."
}
},
{
"_id": {
"$oid": "6384d2bdf29f40634fe77db5"
},
"type": "allergens",
"key": "0",
"parent": "nutrition:panel#0",
"uid": "allergens#0",
"value": {
"type": "contains",
"statement": "contains something"
}
},
{
"_id": {
"$oid": "6384d2bdf29f40634fe77db5"
},
"type": "allergens",
"key": "0",
"parent": "nutrition:panel#0",
"uid": "allergens#0",
"value": {
"type": "something",
"statement": "may contain something"
}
},
{
"_id": {
"$oid": "6384d2bdf29f40634fe77db6"
},
"type": "variants:flavors:raw",
"key": "0",
"parent": "nutrition:panel#0",
"uid": "variants:flavors:raw#0",
"value": [
"hollandaise sauce"
]
}
],
"dataUpdatedDate": {
"$date": {
"$numberLong": "1668025007811"
}
}
}
To something like this, merging objects with the same "type" "b"
{
"_id": {
"$oid": "636c0a8dce0eb36dc13e63b3"
},
"status": "active",
"companies": [
{
"$oid": "5f64bd6785fad3e83771bb4f"
}
],
"whitelists": [],
"taxonomies": [
{
"$oid": "6080538565c2036dbeadcdfa"
},
{
"$oid": "6080538765c2036dbeadce07"
},
{
"$oid": "6080538d65c2036dbeadce44"
},
{
"$oid": "6080538d65c2036dbeadce46"
}
],
"itemType": "unit",
"attributes": [
{
"_id": {
"$oid": "6384d2bdf29f40634fe77db4"
},
"type": "ingredients:statement",
"key": "0",
"parent": "nutrition:panel#0",
"uid": "ingredients:statement#0",
"value": {
"footnotes": [],
"statement": "ingredients: water, butter, lemon juice, wheat flour, egg yolks, sea salt, xanthan gum, turmeric, carotenes."
}
},
{
"_id": {
"$oid": "6384d2bdf29f40634fe77db5"
},
"type": "allergens",
"key": "0",
"parent": "nutrition:panel#0",
"uid": "allergens#0",
"value": [{
"type": "contains",
"statement": "contains something"
},
{
"type": "something",
"statement": "may contain something"
}
]},
{
"_id": {
"$oid": "6384d2bdf29f40634fe77db6"
},
"type": "variants:flavors:raw",
"key": "0",
"parent": "nutrition:panel#0",
"uid": "variants:flavors:raw#0",
"value": [
"hollandaise sauce"
]
}
],
"dataUpdatedDate": {
"$date": {
"$numberLong": "1668025007811"
}
}
}
My background is php/mysql. I understand the basics here just can't put it together. Any help would be greatly appreciated.
Thanks
I have the basics of the typescript started to grab all the appropriate documents. I don't know how to go about combining some of the data and updating it back to mongo
Here is the basic code I am starting with
makeScript({
module,
db: true,
init: async _argv => {
await Pv2.find({
attributes: {
$elemMatch: {
type: 'allergens'
},
},
})
.limit(10)
.cursor()
.eachAsync(async (oldPv2s: Pv2Doc | Pv2Doc[]) => {
// not sure what goes here
});
}
});

Joining only matching elements

I want to join more than two collections in MongoDB . Is it possible to join?
I have 2 collections and I want to join them based by their ids
news collection
{
"lang": "ru",
"news": [{
"title": "title",
"content": "desc",
"image": "path",
"categoryId": {
"$oid": "6171499e0ba3d75082823c9a"
},
"createdAt": {
"$date": "2021-10-21T12:30:33.215Z"
},
"_id": {
"$oid": "61715d7990905adefcf314d1"
}
}]
}
and I have got newscategory collection like that
[
{
"lang": "ru",
"categories": [{
"name": "sdfsdsdcsdf",
"_id": {
"$oid": "6171499e0ba3d75082823c9a"
}
}]
},
{
"lang": "en",
"categories": [{
"name": "ooo",
"_id": {
"$oid": "61712980f8ee795c6a569dcb"
}
}, {
"name": "yuy",
"_id": {
"$oid": "61712980f8ee795c6a569dcc"
}
}, {
"name": "rtyrty",
"_id": {
"$oid": "61712980f8ee795c6a569dcd"
}
}]
}
]
Expected Output:
[
{
"_id": "61715d7990905adefcf314d0",
"lang": "ru",
"news": [
{
"title": "sdf",
"content": "sdf",
"image": "asdas",
"categoryId": "6171499e0ba3d75082823c9a",
"caregory": [
{
"name": "sdfsdsdcsdf",
"_id": "6171499e0ba3d75082823c9a"
},
]
"createdAt": "2021-10-21T12:30:33.215Z",
"_id": "61715d7990905adefcf314d1"
}
],
Match is based on both "category_id" and "categories._id".
How can I achieve result like that ?

JSON:API Matching Collections with its respective Includes

What exactly is the best practice for matching JSON:API data collections with their respective includes. Considering the following code below....
What if I wanted to loop through each venue and display the Owners full information for each Venue Record. Does JSON:API expect me to just search the include array for the matching Owner Record
find(included,data[$i].relationships.owner.data.id);
Would find() loop through the included array to look for the owner that has the matching id as the collection items owner in the relationships object ?
$(data).each(function(item){
var owner = find(included,'owner', item.relationships.owner.data.id)
})
I have not found a resource that explains this or perhapes I am mis understanding the point of json:api. If someone can explain this or point to a resource that relates to my question. I would appreciate it.
{
"links": {
"self": "http://127.0.0.1/api/venues?include=owner"
},
"data": [
{
"id": "5c5b49188fd33c7a989ba9b6",
"type": "venues",
"attributes": {
"name": "Kreiger - Smith",
"address": "69675 Reilly Vista",
"location": {
"type": "Point",
"coordinates": [
-112.110492,
36.098948
]
},
"events": [
{
"_id": "ad52825a8f4812e92f87b8c6",
"name": "Cool Awesome Event!",
"user": "b3daa77b4c04a9551b8781d0",
"id": "ad52825a8f4812e92f87b8c6"
}
],
"created_at": "2019-02-07T14:27:13.207Z",
"updated_at": "2019-02-07T14:27:13.207Z"
},
"relationships": {
"owner": {
"data": {
"id": "b3daa77b4c04a9551b8781d0",
"type": "users"
}
}
}
},
{
"id": "5c5b49188fd33c7a989ba9b7",
"type": "venues",
"attributes": {
"name": "Oberbrunner Inc",
"address": "1132 Kenyon Stravenue",
"location": {
"type": "Point",
"coordinates": [
-112.110492,
36.098948
]
},
"events": [
{
"_id": "ad52825a8f4812e92f87b8c6",
"name": "Cool Awesome Event!",
"user": "b3daa77b4c04a9551b8781d0",
"id": "ad52825a8f4812e92f87b8c6"
}
],
"created_at": "2019-02-07T14:27:13.207Z",
"updated_at": "2019-02-07T14:27:13.207Z"
},
"relationships": {
"owner": {
"data": {
"id": "b3daa77b4c04a9551b8781d0",
"type": "users"
}
}
}
},
{
"id": "5c5b49188fd33c7a989ba9b8",
"type": "venues",
"attributes": {
"name": "Gibson - Muller",
"address": "8457 Hailie Canyon",
"location": {
"type": "Point",
"coordinates": [
-112.110492,
36.098948
]
},
"events": [
{
"_id": "ad52825a8f4812e92f87b8c6",
"name": "Cool Awesome Event!",
"user": "b3daa77b4c04a9551b8781d0",
"id": "ad52825a8f4812e92f87b8c6"
}
],
"created_at": "2019-02-07T14:27:13.208Z",
"updated_at": "2019-02-07T14:27:13.208Z"
},
"relationships": {
"owner": {
"data": {
"id": "a1881c06eec96db9901c7bbf",
"type": "users"
}
}
}
}
],
"included": [
{
"id": "b3daa77b4c04a9551b8781d0",
"type": "users",
"attributes": {
"username": "killerjohn",
"firstname": "John",
"lastname": "Chapman"
}
},
{
"id": "a1881c06eec96db9901c7bbf",
"type": "users",
"attributes": {
"username": "numerical25",
"firstname": "Billy",
"lastname": "Gordon"
}
}
]
}
This is my best possible solution. But is there a better way ? Seems like alot more coding just to find a collections associated included data
axios.get('http://127.0.0.1:3000/api/venues?include=owner').then(function(response) {
var venues = response.data.data;
var data = response.data;
for(x in venues) {
var owner = data.included.find(function(element) {
if(element.id == venues[x].relationships.owner.data.id) {
return element;
}
});
}
});

How to fetch embedded document list as flat array in Mongoose?

I have this model:
"client": {
"title": { "type": "String", "required": true },
"firstname": { "type": "String", "required": true },
"lastname": { "type": "String", "required": true },
"email": { "type": "String", "required": true, "index": { "unique": true } ...
"purchases": [{ "amount": "Number", "productID": "String", "transactionDate": "Date"}],
"complementaryNote": "String"
}
I want to fetch purchases as such:
[{ "amount": "Number", "productID": "String", "transactionDate": "Date"}, { "amount": "Number", "productID": "String", "transactionDate": "Date"} ...]
I use this query:
Client.find(pQuery).sort('-purchases.transactionDate').select('purchases').exec(function(err, pResults) {
pResultCallback(pResults)})
Which gives this result:
[
{
"_id": "569e50cf4895ced01aa55d9c",
"purchases": [
{
"amount": 88761,
"transactionDate": "2016-01-19T15:02:37.800Z",
"productID": "Awesome Fresh Tuna",
"_id": "569e50cf4895ced01aa55d9d"
}
]
},
{
"_id": "569e50df4895ced01aa589ae",
"purchases": [
{
"amount": 66487,
"transactionDate": "2016-01-19T14:02:19.777Z",
"productID": "Licensed Cotton Keyboard",
"_id": "569e50df4895ced01aa589af"
}
]} ... ]
What I want to have instead is this:
[{
"amount": 88761,
"transactionDate": "2016-01-19T15:02:37.800Z",
"productID": "Awesome Fresh Tuna",
"_id": "569e50cf4895ced01aa55d9d"
},
{
"amount": 66487,
"transactionDate": "2016-01-19T14:02:19.777Z",
"productID": "Licensed Cotton Keyboard",
"_id": "569e50df4895ced01aa589af"
} ... ]
So I could eventually order them bt transaction date.
Thanks to anyone trying to help.

Resources