How can I have array filter count from nested array - mongodb

Data:
_id: ObjectId(''),
restaurantId: ObjectId(''),
orderId: ObjectId(''),
reviews: [
{
type: "food",
tags: ["good food", "nice food"]
},
{
type: "pricing",
tags: ["best price", "good price"]
}
]
Group by restaurant id
Get total reviews by type: Ex: food: 3, pricing: 2, ambience: 1
Group by type and tags and get counts:
Ex: food-superb: 1, food-loudt:2, pricing-superb: 2, ambience-superb: 1
Expected result:
{
_id: restaurantId,
types: {
food: 4,
....
},
tags: {
nicePrice: 20
}
}

Related

Filter documents that do not have a property set - Prisma

I have this example data:
const example = [
{
productId: 1,
name: "Husdady",
age: 21
},
{
name: "Vereth",
age: 19
},
{
name: "Mikaela",
age: 17
},
{
name: "Richard",
age: 20
},
{
productId: 4,
name: "Markus",
age: 24
},
{
productId: 8,
name: "Cecilia",
age: 18
},
{
productId: 1,
name: "Anton",
age: 16
}
]
console.log(example)
I have a 'User' model in prisma. So what I must achieve is to filter all those users that do not have a defined productId and also filter by productId.
For example, I want to filter all users that have product Id 1 and also all users that do not have a productId defined
I have tried with the following filter
this.prisma.users.findMany({
where: {
OR: [
{ productId: undefined },
{ productId: 1 }
]
}
})
This query gets me all the users that have '1' as the value of their 'productId' but it does not bring me the users that do not have a defined productId.
The expected result of the above query would be:
const result = [
{
productId: 1,
name: "Husdady",
age: 21
},
{
name: "Vereth",
age: 19
},
{
name: "Mikaela",
age: 17
},
{
name: "Richard",
age: 20
},
{
productId: 1,
name: "Anton",
age: 16
}
]
console.log(result)
I only get the users 'Husdady' and 'Anton'
const resultObtained = [
{
productId: 1,
name: "Husdady",
age: 21
},
{
productId: 1,
name: "Anton",
age: 16
}
]
console.log(resultObtained)
How I can resolve this problem?

Algolia retrieve results by multiple facets

First of all, I am using Algolia JavaScript API Client V3 (Deprecated)
I have the following records
{
category: SEDAN,
manufacturer: Volkswagen,
id: '123'
},
{
category: COUPE,
manufacturer: Renault,
id: '234'
},
{
category: SEDAN,
manufacturer: Fiat,
id: '345'
},
{
category: COUPE,
manufacturer: Peugeot,
id: '456'
},
{
category: SUV,
manufacturer: Volkswagen,
id: '567'
}
I want to query Algolia and get something similar to the following json
{
categories: {
SEDAN: {
count: 2
items: [{
Volkswagen: {
count 1,
items: [{
id: '123'
}]
}
},
{
Fiat: {
count 1,
items: [{
id: '345'
}]
}
}]
},
COUPE: {
count: 2
items: [{
Renault: {
count 1,
items: [{
id: '234'
}]
}
},
{
Peugeot: {
count 1,
items: [{
id: '456'
}]
}
}]
},
SUV: {
count: 1,
items: [{
Volkswagen: {
count 1,
items: [{
id: '567'
}]
}
}]
}
}
}
I have been trying to query Algolia
index
.search({
query: '',
facets: ['category', 'manufacturer'],
attributesToRetrieve: []
})
.then((result) => {
console.log(result.facets);
});
But I am not sure if it is possible to combine the facets
facets added to a query doesn't work that way. It will simply return the record count for each facet value, not the actual records (https://www.algolia.com/doc/api-reference/api-parameters/facets/)
You can create filters around facets and use those to display results by facet value, but there isn't a way to build a single response JSON that is already grouped by facets like you show above. https://www.algolia.com/doc/api-reference/api-parameters/filters/

Group by and retain original fields

I see in mongodb aggregations, specially in $group we can use accumulator to create new fields. But i want the old keys
Suppose i have this data
[
{ name: "My Plan 101", billingCycle: 'day', amount: 1, credits: 100, price: 7 },
{ name: "My Plan 102", billingCycle: 'day', amount: 1, credits: 150, price: 10 },
{ name: "My Plan 103", billingCycle: 'day', amount: 2, credits: 150, price: 15 },
{ name: "My Plan 104", billingCycle: 'month', amount: 3, credits: 150, price: 15 },
{ name: "My Plan 105", billingCycle: 'month', amount: 3, credits: 200, price: 20 },
]
Then the aggregation should be like
[
'day': {
'1': [{ name: 'My Plan 101' }, { name: 'My Plan 102' }],
'2': [{ name: 'My Plan 103' }]
},
'month': {
'3': [{ name: 'My Plan 104' }, { name: 'My Plan 105' }]
}
]
I tried a lot with mongodb aggregation but couldn't solve it so I used lodash for this.
let plans = await Plan.find()
plans = _.groupBy(plans, 'billingCycle');
for (const billingCyle in plans) {
let $plans = plans[billingCyle];
plans[billingCyle] = _.groupBy($plans, "amount")
}
console.log(plans)
The above snipped has solved my problem

Populate data in mongoose on collections(generated by strapi.io)

I have 3 collections in mongodb generated by strapi.io, I want customize query populate data by mongoose in my project. But i cannot populate data like strapi result.
Category collection:
{
_id: "5d10a731c5077836540bebf0",
posts: [
"5d10a6fbc5077836540bebed",
"5d10af45c5077836540bebf1"
],
name: "Support",
ids: "support",
keywords: "support",
des: "support des",
createdAt: "2019-06-24T10:34:25.383Z",
updatedAt: "2019-06-24T11:08:53.475Z",
__v: 0,
id: "5d10a731c5077836540bebf0"
}
Post collection:
{
_id: "5d10af45c5077836540bebf1",
ids: "test",
title: "this is test",
des: "test",
keywords: "test",
body: "testtesttesttesttest",
createdAt: "2019-06-24T11:08:53.467Z",
updatedAt: "2019-06-24T11:08:53.477Z",
__v: 0,
id: "5d10af45c5077836540bebf1"
}
FileUpload collection:
{
_id: "5d10af45c5077836540bebf2",
name: "THUMBNAIL.png",
sha256: "0bP9PI3R_ygF07cLhg5U-syLeVCz4ZbBrwZZi2OtPL4",
hash: "9f4e8bbd13b94f2baa3d26b335124717",
ext: ".png",
mime: "image/png",
size: "297.33",
url: "/uploads/9f4e8bbd13b94f2baa3d26b335124717.png",
provider: "local",
related:
[
{
_id: "5d10af45c5077836540bebf3",
ref: "5d10af45c5077836540bebf1",
kind: "Post",
field: "pic"
}
],
createdAt: "2019-06-24T11:08:53.501Z",
updatedAt: "2019-06-24T11:08:53.505Z",
__v: 0,
id: "5d10af45c5077836540bebf2"
}
I can populate "posts" on Category model with
Cat.find().populate({path: 'posts',model: 'Post'})
But I can not populate "categories" and "pic" on Post model.
How can I populate "categories" and "pic" like this result of strapi:
{
_id: "5d10af45c5077836540bebf1",
ids: "test",
title: "this is test",
des: "test",
keywords: "test",
body: "testtesttesttesttest",
createdAt: "2019-06-24T11:08:53.467Z",
updatedAt: "2019-06-24T11:08:53.477Z",
__v: 0,
id: "5d10af45c5077836540bebf1",
pic: {
_id: "5d10af45c5077836540bebf2",
name: "THUMBNAIL.png",
sha256: "0bP9PI3R_ygF07cLhg5U-syLeVCz4ZbBrwZZi2OtPL4",
hash: "9f4e8bbd13b94f2baa3d26b335124717",
ext: ".png",
mime: "image/png",
size: "297.33",
url: "/uploads/9f4e8bbd13b94f2baa3d26b335124717.png",
provider: "local",
related: [
"5d10af45c5077836540bebf1"
],
createdAt: "2019-06-24T11:08:53.501Z",
updatedAt: "2019-06-24T11:08:53.505Z",
__v: 0,
id: "5d10af45c5077836540bebf2"
},
categories:
[
{
posts: [
"5d10a6fbc5077836540bebed",
"5d10af45c5077836540bebf1"
],
_id: "5d10a731c5077836540bebf0",
name: "Support",
ids: "support",
keywords: "support",
des: "support des",
createdAt: "2019-06-24T10:34:25.383Z",
updatedAt: "2019-06-24T11:08:53.475Z",
__v: 0,
id: "5d10a731c5077836540bebf0"
}
]
}
you have to create another collection inside the post. as like you have an independent collection then you can populate. for example :
Post: {body: ,title: ,Image : { image: {name : , etc }}}
this can be a collection inside collection. the above code is not correct you have to mention your own fields.

In Sails.js v0.10 after populating data as a value i get [Getter/Setter]

After populating data i get result not as expected.
Example:
//User.js Model
module.exports = {
attributes: {
//"id" attribute is here primary key by default
schools: { collection: 'School', via: 'id' },
name: { type: 'String', trim: true, required: true }
}
}
And this is my School.js
//School.js Model
module.exports = {
attributes: {
//"id" attribute is here primary key by default
name: { type: 'String', trim: true, required: true },
}
}
My User entity data looks like this:
//User document in MongoDB
{
_id: 1,
name: "Foo",
schools: [1,2,3]
}
My School entity data looks like this:
//School documents in MongoDB
{
_id: 1,
name: "School 1"
}
{
_id: 2,
name: "School 2"
}
{
_id: 3,
name: "School 3"
}
Now i want to populate the schools. I do it like this:
User.find().populate("schools").exec(function(err, res){ console.log(res[0]) });
And this is what i get as a result:
{
schools: [Getter/Setter],
name: "Foo",
id: 1
}
Expected:
{
schools: [
{id: 1, name: "School 1"},
{id: 2, name: "School 2"},
{id: 3, name: "School 3"}
],
name: "Foo",
id: 1
}
How can i get expected results?
I use MongoDB as data storage.
Versions:
Sails.js: v0.10.0-rc5
Waterline: v0.10.0-rc7
sails-mongo: 0.10.0-rc2
Those are the expected results when using console.log! If you want to see the expanded object, do:
console.log(res[0].toObject());