how to exclude value when certain property not exist in loopback - node.js

I'm getting below response from loopback when I filter with below api:
http://localhost:8001/myloopback/productDetails?q=product.productId==apple
I want to exclude the data which doesn't have property "productModel" - how can we achieve this in Loopback rest API. Is there any option to exclude property from the rest API query param? I'm new to Loopback - it would be really grateful if someone can help me to fix this
{
"myData": [{
"data": {
"product": {
"productId": "apple",
"productName": "iPhone",
"productModel": "6s"
},
"statusCode": "active",
"date": "2018-08-07T00:00:00.000Z"
},
"links": [{
"productUrl": "test"
},
{
"productImage": "test"
}
],
"info": {}
},
{
"data": {
"product": {
"productId": "apple",
"productName": "iPhone",
"productModel": "7"
},
"statusCode": "active",
"date": "2018-08-07T00:00:00.000Z"
},
"links": [{
"productUrl": "test"
},
{
"productImage": "test"
}
],
"info": {}
},
{
"data": {
"product": {
"productId": "apple",
"productName": "Macbook"
},
"statusCode": "active",
"date": "2018-08-07T00:00:00.000Z"
},
"links": [{
"productUrl": "test"
},
{
"productImage": "test"
}
],
"info": {}
}
],
"metadata": {
"count": 3,
"offset": 0
}
}

There is no such inbuilt function to do that but you can use multiple Fields filter to achieve that.

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.`

Shopware 6 API Create a Product with Variants

I created my products with the API and all is ok. But we have some products with variants. These looking good in the backend, but in the frontend shopware doesn't display the product. Where is my error in the payload?
Thank you for helping me!
Here is my code:
{
"write-product":
{
"entity": "product",
"action": "upsert",
"payload": [
{
"id": "17cf3bac8966cc4de52aa557f59e5fd3",
"active": true,
"deliveryTimeId": "1e04777cb17d4396b424129b2d8e3a79",
"description": "Produktbeschreibung",
"ean": null,
"featureSetId": null,
"manufacturerId": null,
"manufacturerNumber": null,
"minPurchase": 1,
"name": "Produktname",
"productNumber": "Nummer",
"releaseDate": "2022-09-28T14:12:18.03",
"stock": 999999,
"taxId": "2842fd17d5a7424795853e7fe7072448",
"price":[
{
"currencyId": "b7d2554b0ce847cd82f3ac9bd1c0dfca",
"net": 13.77,
"gross": 16.39,
"linked": false
}
],
"categories":[
{
"id":"648875017ed54637a41c8c1f972c239f",
"name":"Kategoriename"
}
],
"configuratorSettings": [
{
"productId": "17cf3bac8966cc4de52aa557f59e5fd3",
"optionId": "2ab9df7d2dd94ad38eb53cf7dcf16e56"
},
{
"productId": "17cf3bac8966cc4de52aa557f59e5fd3",
"optionId": "0fcf784970ea448bac1f7db542df9b86"
},
{
"productId": "17cf3bac8966cc4de52aa557f59e5fd3",
"optionId": "0014aa7dc0b644b78cd4b39193f580b4"
}
],
"properties": [
{
"groupId":"ca74efd59b5545f2b4e4f532c217b2ce",
"name": "1,0 m"
},
{
"groupId":"fa7f63d697d44684a5bb74ba5a73d3c7",
"name": "2,0 mm"
},
{
"groupId":"a431bc98fe0149c3bf3ececf629e9740",
"name": "Material"
}
],
"visibilities": [
{
"id": "17cf3bac8966cc4de52aa557f59e5fd3",
"salesChannelId": "840a0403b56240b6a11502af1d46521e",
"visibility": 30
}
],
"coverId": "0f9ab99638bd0ea5e907234bf425819e",
"cover":
{
"mediaId": "0f9ab99638bd0ea5e907234bf425819e"
},
"media": [
{
"media":
{
"id": "0f9ab99638bd0ea5e907234bf425819e",
"position": 0,
"mediaFolderId": "d8f460870e454c0bbca540614ca30029",
"alt": "Bildname",
"description": "Bildbeschreibung",
"title": "Bildtitel"
}
}
],
"children": [
{
"id": "25c8876fb7577cad78df7539737d25ce",
"active": true,
"deliveryTimeId": "1e04777cb17d4396b424129b2d8e3a79",
"description": "Beschreibung Kindelement",
"ean": null,
"featureSetId": null,
"manufacturerId": null,
"manufacturerNumber": null,
"minPurchase": 1,
"name": "Name Kindelement",
"productNumber": "Nummer Kindelement",
"releaseDate": "2022-09-16T14:14:31.41",
"stock": 999999,
"taxId": "2842fd17d5a7424795853e7fe7072448",
"price":[
{
"currencyId": "b7d2554b0ce847cd82f3ac9bd1c0dfca",
"net": 82.63,
"gross": 98.33,
"linked": false
}
],
"categories":[
{
"id":"648875017ed54637a41c8c1f972c239f",
"name":"Kategoriename"
}
],
"configuratorSettings": [
{
"productId": "25c8876fb7577cad78df7539737d25ce",
"optionId": "b224173313cb4f8da03578ef83702751"
},
{
"productId": "25c8876fb7577cad78df7539737d25ce",
"optionId": "0fcf784970ea448bac1f7db542df9b86"
},
{
"productId": "25c8876fb7577cad78df7539737d25ce",
"optionId": "0014aa7dc0b644b78cd4b39193f580b4"
}
],
"properties": [
{
"groupId":"ca74efd59b5545f2b4e4f532c217b2ce",
"name": "6,0 m"
},
{
"groupId":"fa7f63d697d44684a5bb74ba5a73d3c7",
"name": "2,0 mm"
},
{
"groupId":"a431bc98fe0149c3bf3ececf629e9740",
"name": "Material"
}
],
"parentId": "17cf3bac8966cc4de52aa557f59e5fd3",
"options": [
{
"id":"b224173313cb4f8da03578ef83702751",
"group": {
"id":"ca74efd59b5545f2b4e4f532c217b2ce"}
},
{
"id":"0fcf784970ea448bac1f7db542df9b86",
"group": {
"id":"fa7f63d697d44684a5bb74ba5a73d3c7"}
},
{
"id":"0014aa7dc0b644b78cd4b39193f580b4",
"group": {
"id":"a431bc98fe0149c3bf3ececf629e9740"
}
}
],
"visibilities": [
{
"id": "25c8876fb7577cad78df7539737d25ce",
"salesChannelId": "840a0403b56240b6a11502af1d46521e",
"visibility": 30
}
],
"media": [
{
"media": {
"id": "fba6e74294488b554aa88f7dcf6f38f2",
"position": 0,
"mediaFolderId": "d8f460870e454c0bbca540614ca30029",
"alt": "Bildname Kindelement",
"description": "Beschreibung Kindelement",
"title": "Bildtitel Kindelement"
}
}
]
}
]
}
]
}
}
I'm searching in different boards and looking in the API Browser, Using swagger for the api and so on. No result. I need the main-product and the variants be visible in the frontend. At the moment all products with variants are invisible in the frontend.
Double check your Visibility & Categories. It could be them. Also, maybe you need to re-index after creation?
You have way too much stuff going on at the moment for us to help you out.
Try to split your calls into smaller chunks (for us). Also start with less data when creating a product. With every step check the frontend. Can you even create a simple product & display it on the frontend? Yes? Then try create the most basic child that inherits most of the visible parent.
For a child you do not need much data, e.g.
{
"write-product": {
"entity": "product",
"action": "upsert",
"payload": [
{
"parentId": "",
"options": [
{
"id": ""
}
],
"price": [
{
"currencyId": "{{gen_salesChannel_currency_id}}",
"gross": 0.0,
"net": 0.0,
"linked": false
}
]
"productNumber": "",
"stock": 999
}
]
}
}
Another tip is to use the network tab in the admin to see how it creates these relationships. Here is an image of how it looks like:

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;
}
});
}
});

Query to retrieve every subdocument alone without passing parent id using Mongoose

Here i like to explain my problem.
How can i write a mongoose query to retrieve every subdocument from JSON without passing parent_id.
[
{
"_id": "56a320003fe17cc7363dd0d7",
"name": "Leanna Jacobson",
"gender": "female",
"friends": [
{
"id": 0,
"name": "Riley Case"
},
{
"id": 1,
"name": "Herman Carter"
},
{
"id": 2,
"name": "Pacheco Woodard"
}
]
},
{
"_id": "56a3200001501cfa1ea2641d",
"name": "Juliana Bonner",
"gender": "female",
"friends": [
{
"id": 0,
"name": "Keller Woodward"
},
{
"id": 1,
"name": "Fern Knight"
},
{
"id": 2,
"name": "Cain Richards"
}
]
},
{
"_id": "56a3200006864c78ecb1aeed",
"name": "Gena Stark",
"gender": "female",
"friends": [
{
"id": 0,
"name": "Kate Franco"
},
{
"id": 1,
"name": "Araceli Mcclure"
},
{
"id": 2,
"name": "Molly Nelson"
}
]
},
{
"_id": "56a320006d868155161038b6",
"name": "Eve Gonzalez",
"gender": "female",
"friends": [
{
"id": 0,
"name": "Pam Lang"
},
{
"id": 1,
"name": "Christy Marks"
},
{
"id": 2,
"name": "Donovan Warren"
}
]
},
{
"_id": "56a3200066b94852f5680568",
"name": "Coleman Wooten",
"gender": "male",
"friends": [
{
"id": 0,
"name": "Roberta Olson"
},
{
"id": 1,
"name": "Roseann Reid"
},
{
"id": 2,
"name": "Kerri Russell"
}
]
}
]
Here i need to retrieve every friends details from the subdocument array friends for every parent.
so how can i write query for this?????
Suppose the name of your schema is Person, try this one.
//find all document, only select `friends` field from every document
Person.find({}, 'friends', function (err, friends) {
// the return friends is the [[friends], [friends], ...]
});

Resources