How to search by certain objects keys values - mongodb

First sorry for my bad English!
Suppose this document looks like this:
{
"id": "598969845c274c145a935994",
"contract_ref": "ABCDEF1234",
"currency": "USD",
"per_unit_cost": [
{
"size": 22,
"price": 1000
},
{
"size": 42,
"price": 1500
}
]
},
{
"id": "598969845c274c145a93599b",
"contract_ref": "ABCDEF1234",
"currency": "USD",
"per_unit_cost": [
{
"size": 22,
"price": 1234
}
]
}
How do you find 'per_unit_cost.size' that exactly match with '$all' [22], i.e. retrieve the second document and ignore the first?

Related

How to handle tags and hierarchies in ag-grid table data structure?

I would like to use the pivot feature, but since my data has a lot of tags (1-level nested), I'm not sure how to make ag-grid parse this data and recognize it.
Here's the data for example:
[
{
"_id": "1",
"symbol": "sym1",
"open_date": "2019-04-23",
"_type": "t1",
"currency": "USD",
"quantity": 456,
"tags": [
{
"tag": "G_TAG1",
"group_name": "Group_1",
},
{
"tag": "G_TAG2",
"group_name": "Group_1",
}
],
},
{
"_id": "2",
"symbol": "sym2",
"open_date": "2029-01-11",
"_type": "t2",
"currency": "EUR",
"quantity": 312,
"tags": [
{
"tag": "G3_TAG-ABC",
"group_name": "Group_3",
},
{
"tag": "G2_TAG-DEF",
"group_name": "Group_2",
},
{
"tag": "G_TAG3",
"group_name": "Group_1",
}
],
},
{
"_id": "3",
"symbol": "sym3",
"open_date": "2010-12-01",
"_type": "t3",
"currency": "AUD",
"quantity": 123,
"tags": [
{
"tag": "G_TAG3",
"group_name": "Group_1",
}
],
}
]
In columnDefs, I defined tags column like this:
{field:'tags',
enablePivot: true,
children: [
{
headerName: "Tag", valueGetter: params => params.data.tags.map(x => x.tag),
enablePivot: true,
},
{
headerName: "Tag Group", valueGetter: params => params.data.tags.map(x => x.group_name),
enablePivot: true,
},
]
},
Each data point has a list of tags and each tag can belong to a group or not. So the top-most hierarchy is the tag group and below that are the individual tags.
Is there a way aggrid can recognize this or a way I can transform the data so visualize per-tag or per-group statistics? For example, can I view stats grouped by tag group > tag > symbol?

How can I compute the income of the sellerId of the products?

I'm trying to learn the advanced mongodb+mongoose function, so this is the result of my orders, and what I'm trying to do here is to compute the total amounts related to the sellerId
So in this one, I have two documents, the document 1 have an amount of 99 and and the other one is 11
so I need to get the sum of two. I've been searching and found the aggregate, but I can't figure out how I can combine the two documents.
[
{
"_id": "6360d1d0bd860240e2589564",
"userId": "6360cf687e186ebe29ab2a29",
"products": [
{
"productId": "6360cdd166480badb8c1e05b",
"quantity": 1,
"sellerId": "6360c6ed05e1e99034b5f7eb",
"_id": "6360d1d0bd860240e2589565"
}
],
"amount": 99,
"location": "asdsad",
"time": "asdsad",
"status": "pending",
"tax": 0.99,
},
{
"_id": "6360d7978044f3048e59bf34",
"userId": "6360d50dbd860240e258c585",
"products": [
{
"productId": "6360d7528044f3048e59bb6c",
"quantity": 1,
"sellerId": "6360d4d5bd860240e258c582",
"_id": "6360d7978044f3048e59bf35"
},
{
"productId": "6360d7868044f3048e59bd8c",
"quantity": 1,
"sellerId": "6360d4d5bd860240e258c582",
"_id": "6360d7978044f3048e59bf36"
}
],
"amount": 11,
"location": "Gym",
"time": "8:00 AM",
"status": "pending",
"tax": 0.11,
}
]
This might helps.
db.collection.aggregate([
{
$group: {
_id: null,
count: {
$sum: "$amount"
}
}
}
])

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

Subtotal is not calculated correctly at order preview

I want to create a chatbot with Dialogflow and Google Assistant along with Google Transactions API for enabling a user to order some items. For now my agent contains the following four intents:
Default Welcome Intent (text response: Hello, do you want to buy a chocolate box?)
Default Fallback Intent
Int3 (training phrase: Yes, I want, fulfilment: enabled webhook)
Int4 (event: actions_intent_TRANSACTION_DECISION, fulfilment: enabled webhook)
I am using Dialogflow Json instead of Node.js to connect my agent with Transactions API. I want to build a cart and an order for the user by using finally that the user meets the transaction requirements by using the actions.intent.TRANSACTION_DECISION action of Google actions. For this reason, following Google docs, when Int3 is triggered I am using a webhook which connect Google Assistant my back-end which sends back the following json (to trigger actions.intent.TRANSACTION_DECISION) :
{
"fulfillmentText": "This is your order preview:",
"payload": {
"google": {
"expectUserResponse": true,
"isSsml": false,
"noInputPrompts": [],
"systemIntent": {
"data": {
"#type": "type.googleapis.com/google.actions.v2.TransactionDecisionValueSpec",
"orderOptions": {
"requestDeliveryAddress": true
},
"paymentOptions": {
"actionProvidedOptions": {
"displayName": "VISA **** **** **** 3235",
"paymentType": "PAYMENT_CARD"
}
},
"proposedOrder": {
"cart": {
"lineItems": [
{
"description": "Book",
"id": "1",
"name": "Book",
"price": {
"amount": {
"currencyCode": "USD",
"nanos": 0,
"units": 31
},
"type": "ACTUAL"
},
"quantity": 2,
"subLines": [],
"type": "REGULAR"
}
],
"merchant": {
"id": "Amazon",
"name": "Amazon"
},
"otherItems": []
},
"id": "<UNIQUE_ORDER_ID>",
"otherItems": [
{
"id": "Subtotal",
"name": "Subtotal",
"price": {
"amount": {
"currencyCode": "USD",
"nanos": 0,
"units": 62
},
"type": "ACTUAL"
},
"type": "SUBTOTAL"
},
{
"id": "Delivery fees",
"name": "Delivery fees",
"price": {
"amount": {
"currencyCode": "USD",
"nanos": 0,
"units": 10
},
"type": "ACTUAL"
},
"type": "FEE"
}
],
"totalPrice": {
"amount": {
"currencyCode": "USD",
"units": 72
},
"type": "ACTUAL"
}
}
},
"intent": "actions.intent.TRANSACTION_DECISION"
}
}
}
}
However, I am getting the following errors on Google Assistant simulator:
MalformedResponse
expected_inputs[0].possible_intents[0].input_value_data.transaction_decision_value_spec.proposed_order: subtotal price is not sum of regular lineItems. Expected-> Sum of line item price: units: 31 nanos: 0 currency: USD Actual-> Provided total price: units: 62 nanos: 0 currency: USD.
MalformedResponse
expected_inputs[0].possible_intents[0].input_value_data.transaction_decision_value_spec.proposed_order: total price is not sum of lineItems and otherItems. Expected-> Sum of line item price: units: 42 nanos: 0 currency: USD Actual-> Provided total price: units: 72 nanos: 0 currency: USD.
Why I am getting this error since I have specified that "quantity": 2 in the respective lineItem?
It is very obvious that the subtotal should be units: 62 since I have ordered "quantity": 2 products of units: 31 price each...
The final order preview looks like this (this is borrowed from Google docs examples):
The answer seems to be in the error message: the API expects a each line item's price to be a sum of the total items in that line item instead of a quantity price (relevant error phrase: "Sum of line item price"). The corrected JSON should look something like this:
{
"fulfillmentText": "This is your order preview:",
"payload": {
"google": {
"expectUserResponse": true,
"isSsml": false,
"noInputPrompts": [],
"systemIntent": {
"data": {
"#type": "type.googleapis.com/google.actions.v2.TransactionDecisionValueSpec",
"orderOptions": {
"requestDeliveryAddress": true
},
"paymentOptions": {
"actionProvidedOptions": {
"displayName": "VISA **** **** **** 3235",
"paymentType": "PAYMENT_CARD"
}
},
"proposedOrder": {
"cart": {
"lineItems": [
{
"description": "Book",
"id": "1",
"name": "Book",
"price": {
"amount": {
"currencyCode": "USD",
"nanos": 0,
"units": 62
},
"type": "ACTUAL"
},
"quantity": 2,
"subLines": [],
"type": "REGULAR"
}
],
"merchant": {
"id": "Amazon",
"name": "Amazon"
},
"otherItems": []
},
"id": "<UNIQUE_ORDER_ID>",
"otherItems": [
{
"id": "Subtotal",
"name": "Subtotal",
"price": {
"amount": {
"currencyCode": "USD",
"nanos": 0,
"units": 62
},
"type": "ACTUAL"
},
"type": "SUBTOTAL"
},
{
"id": "Delivery fees",
"name": "Delivery fees",
"price": {
"amount": {
"currencyCode": "USD",
"nanos": 0,
"units": 10
},
"type": "ACTUAL"
},
"type": "FEE"
}
],
"totalPrice": {
"amount": {
"currencyCode": "USD",
"units": 72
},
"type": "ACTUAL"
}
}
},
"intent": "actions.intent.TRANSACTION_DECISION"
}
}
}
}

How JSON format for calling Checking out REST Api of Broadleaf commerce looks like

I have followed setting up Broadleaf to make it running by following its documentation (http://docs.broadleafcommerce.org/current/REST-Tutorials.html). I wanted to do checking out the cart using REST Api; that's, /cart/checkout. Therefore, I looked inside the code in order to understand how the JSON format being sent will look alike. By looking through the code, I found that it is required to pass the JSON data as shown following:
{
"paymentInfo": {
"id": ,
"orderId": ,
"type": ,
"address": {
"id":
"firstname":
"lastname":
"addressLine1":
"addressLine2":
"city":
"state":
"country":
"postalCode":
},
"phone": "",
"additionalFields": "",
"amount": "",
"amountItems": "",
"customerIpAddress": "",
"referenceNumber": ""
},
"referenced": {
"id": "",
"referenceNumber": "",
"type": "",
"pan": "",
"cvvCode:" "",
"expirationMonth": "",
"expirationYear": "",
"accountNumber": "",
"routingNumber": "",
"pin": ""
}
}
However, I have no idea such the JSON data looks like. Therefore, if anyone who has ever used the api, please help me by showing an example of data in order to make the request. Looking forward to the answers.
Thanks in advance.
All of our REST APIs are exposed through a 'wrapper' concept. For instance, there is a CustomerWrapper, OrderWrapper, etc. These wrappers define which properties are serialized back and forth with the REST APIs.
For your specific case, you should look at PaymentReferenceMapWrapper.
{
"id": 1751,
"status": "IN_PROCESS",
"totalTax": {
"amount": "0.00",
"currency": "INR"
},
"totalShipping": {
"amount": "0.00",
"currency": "INR"
},
"subTotal": {
"amount": "860.00",
"currency": "INR"
},
"total": {
"amount": "860.00",
"currency": "INR"
},
"customer": {
"id": 2600
},
"orderItems": [
{
"id": 1752,
"name": "abc",
"quantity": 2,
"retailPrice": {
"amount": "430.00",
"currency": "INR"
},
"salePrice": {
"amount": "430.00",
"currency": "INR"
},
"orderId": 1751,
"categoryId": 10300,
"skuId": 10212,
"productId": 10212,
"isBundle": false,
"orderItemPriceDetails": [
{
"id": 1752,
"totalAdjustmentValue": {
"amount": "0.00",
"currency": "INR"
},
"totalAdjustedPrice": {
"amount": "860.00",
"currency": "INR"
},
"quantity": 2,
"adjustments": []
}
],
"isDiscountingAllowed": true
}
],
"fulfillmentGroups": [
{
"id": 1502,
"orderId": 1751,
"total": {
"amount": "860.00",
"currency": "INR"
},
"fulfillmentGroupItems": [
{
"id": 1752,
"fulfillmentGroupId": 1502,
"orderItemId": 1752,
"totalTax": {
"amount": "0.00",
"currency": "INR"
},
"quantity": 2,
"totalItemAmount": {
"amount": "860.00",
"currency": "INR"
}
}
]
}
],
"payments": [
{
"id": 601,
"orderId": 1751,
"type": "COD",
"amount": "860.00",
"currency": "INR",
"gatewayType": "Passthrough",
"transactions": [
{
"id": 601,
"orderPaymentId": 601,
"type": "AUTHORIZE_AND_CAPTURE",
"success": true,
"amount": "860.00",
"currency": "INR"
}
]
}
]
}
This is the same json you will get from /cart?customerId="" with method GET after successfully executing /cart/checkout/payment