MongoDB: self-referencing correctly? - sql

I'm coming from SQL and very new to Mongo. I'm trying to put everybody in my members collection.
This is daddy:
{
"_id":{"$oid":"629f50805d9928ddfeeda108"}, /* auto generated */
"email": "parent1234567#yahoo.com",
"password":"hA$h3dP#zZw0rdstring",
"memberType":"parent",
"fullName": "Senior Rodriguez",
"birthday": "1980-12-31",
"phone": "514-555-9988",
"address": [
{"billing":"100 rue barré, montreal qc h3a1p2"},
{"shipping":"3-1107 rue amherst, montreal qc h2e1g8"}
],
"enrolledIn": [],
"notes": [],
"parentId":[] }
This is the kid:
{
"_id":{"$oid":"729f50805d9928ddfeeda109"}, /* auto generated */
"email": "",
"password":"",
"memberType":"child",
"fullName": "Junior Rodriguez",
"birthday": "2012-01-01",
"phone": "",
"address": [],
"enrolledIn": [
{"courseName":"summer camp sc220601a",
"startDate":"2022-06-07",
"endDate":"2022-08-01",
"notes":"Monday to Friday, 9am-4pm"},
{"courseName":"piano tue 1730-1800 pia220603a",
"startDate":"2022-05-07",
"endDate":"2022-08-01",
"notes":"Every Tuesdays, 5:30pm-6:00pm"}
],
"notes": ["allergic to peanuts", "lactose intolerant"],
"parentId":{"_id":{"$oid":"629f50805d9928ddfeeda108"}} /* manually created */ }
Is this the proper way to self-reference another _id in the same collection?
Sorry I still kinda think like SQL, and I can't help but also think about other questions like, what do I do to enrolledIn when I change the details of the courses? For example summer camp sc220601a got cancelled? And should I put the password on this collection so I can use this same collection for authentication?

Related

Why one entity extratcted different values in different forms in one intent

I have my Dialogflow es agent working weid now.
Previously, I was training one intent understanding how many bedrooms the enduser mentioned, so I have an entity relating to that called $sc_bed It should be a list as a checked the "is list" in action and parameters
action and parametes I have for this Intent
Also I have 3 output contexts with this intent and store the key parameters mentioned
contexts
For my understanding, the sc_bed entity should be stored in all 3 contexts I mentioned above, with same value and format(which should be list rather than string)
However, the log shows that the entity in different contexts are different in both format and value, I will shoe the log here.
"name": "projects/xxxx/locations/global/agent/sessions/f4xxxxxxxx/contexts/search_criteria",
"lifespanCount": 49,
"parameters": {
"is_visitor": "yes",
"lead_first_name": "Test Lead",
"sc_location": [],
"clarify_renter.original": "",
"lead_email.original": "",
"buyer_reply_message2": "I’d like to get a little more information from you if that's ok.",
"sc_timeframe.original": [],
"buyer_interested_listing.original": "",
"conversation.original": "",
"lead_type": "renter",
"buyer_reply_message": "I've got your preferences down here and I'll set up a search for places you might like.",
"seller_market_snapshot.original": "",
"lead_first_name.original": "",
"assigned_agent_name.original": "",
**"sc_bed": {
"amount": 3
},**
"assigned_agent_name": "Test Assigned Agent",
"reply_message_timeframe": "We would love to assist you with the renting process!",
"sc_maxPrice.original": [],
"reminder_price_reduced_link": "chime.me/pricereduced",
"sc_timeframe_range": [],
"search_listing_result.original": "",
"sc_propertyType": [],
"sc_priceRange": [],
"sc_maxPrice": [],
}
},
{
"name": "projects/newagent-mxiwbg/locations/global/agent/sessions/f47970c6-49b2-c8a9-7aff-60316c185007/contexts/general_info",
"lifespanCount": 49,
"parameters": {
"chime_action_add_tag": "AI: Interested",
"buyer_reply_message2.original": "",
"follow_up_event.original": "",
"conversation": "renter_home_search",
"conversation.original": "",
"sc_feature.original": [],
"sc_location": [],
"lead_type": "renter",
"reply_message_timeframe.original": "",
"sc_priceRange": [],
"buyer_reply_message.original": "",
"sc_approxPrice": "",
"sc_propertyType": [],
"sc_propertyType.original": [],
"clarify_renter.original": "",
"sc_timeframe_range": [],
"clarify_renter": "Yes",
"sc_maxPrice.original": [],
**"sc_bed": [
{
"amount.original": [
"2",
"3"
],
"amount": [
2,
3
]
}
],
"sc_bed.original": [
"2 or 3 bedroom"
],**
}
},
What I am expecting are 2 points:
The entity in different contexts should be the same one at least for the entity extracted in this exactly same one intent
The entity info should keep the format I checked which is a list not a string.

How to keep an item unique on single attribute in Redis JSON?

I have a redis JSON collection and it must to store items unique.
[
{
"name": "John",
"tId": 13,
"phone": "124",
"city":"LA"
},
{
"name": "Jane",
"tId": 14,
"phone": "256",
"city":"NYC"
}
]
In this JSON, I want to keep name unique. The other attributes can be duplicate. However, all collection must only 1 item with name John or whatever.
What kind of index do that? Do I need to use RedisBloom?

How to query nested arrays in a postgres json column?

I have some json similar to the json below stored in a postgres json column. I'm trying query it to identify some incorrectly entered data. I'm basically looking for addresses where the house description is the same as the house number. I can't quite work out how to do it.
{
"timestamp": "2014-10-23T16:15:28+01:00",
"schools": [
{
"school_id": "1",
"addresses": [
{
"town": "Birmingham",
"house_description": "1",
"street_name": "Parklands",
"addr_id": "4",
"postcode": "B5 8KL",
"house_no": "1",
"address_type": "UK"
},
{
"town": "Plymouth",
"house_description": "Flat a",
"street_name": "Fore Street",
"addr_id": "2",
"postcode": "PL9 8AY",
"house_no": "15",
"address_type": "UK"
}
]
},
{
"school_id": "2",
"addresses": [
{
"town": "Coventry",
"street_name": "Shipley Way",
"addr_id": "19",
"postcode": "CV8 3DL",
"house_no": "662",
"address_type": "UK"
}
]
}
]
}
I have written this sql which will find where the data matches:
select *
FROM title_register_data
where address_data->'schools'->0->'addresses'->0->>'house_description'=
address_data->'schools'->0->'addresses'->0->>'house_no'
This obviously only works on the first address on the first school. Is there a way of querying all of the addresses of every school?
Use jsonb_array_elements() in lateral, join as many times as the depth of a json array which elements you want to compare:
select
schools->>'school_id' school_id,
addresses->>'addr_id' addr_id,
addresses->>'house_description' house_description,
addresses->>'house_no' house_no
from title_register_data,
jsonb_array_elements(address_data->'schools') schools,
jsonb_array_elements(schools->'addresses') addresses
where addresses->>'house_description' = addresses->>'house_no';
school_id | addr_id | house_description | house_no
-----------+---------+-------------------+----------
1 | 4 | 1 | 1
(1 row)

BigCommerce Stencil - Retrieve Product Custom Fields in Product Card

I'm trying to retrieve each product's custom_fields data on the category list pages in BigCommerce Stencil. This documentation here, and the JSON product representation below, would suggest this is not possible.
It is hard for me to believe this functionality would be left out of Stencil, considering it was available in Blueprint.
{
"id": 691,
"name": "Archipelago Botanicals - Pomegranate Lip Gloss",
"url": "http://******.mybigcommerce.com/archipelago-botanicals-pomegranate-lip-gloss/",
"brand": {
"name": "Archipelago Botanicals"
},
"rating": 0,
"availability": "",
"summary": "Pomegranate lip gloss leaves lips hydrated, shiny and smooth. Apply liberally.Net Weight: 0.42 oz. / 12 g Mineral Oil, Polybutene, Octyidodecanol, Butylene, Ethylene, Styrene Copolymer, Ethylene, Propylene, Styrene Copolymer, C18-36 Acid Triglycerides, Fr",
"image": {
"data": "https://cdn3.bigcommerce.com/*****/images/stencil/{:size}/products/691/41439/arbopolipgl__77026.1464267682.jpg?c=2",
"alt": "Archipelago Botanicals - Pomegranate Lip Gloss"
},
"date_added": "Apr 7th 2016",
"qty_in_cart": 0,
"pre_order": false,
"has_options": false,
"show_cart_action": false,
"price": {
"without_tax": {
"formatted": "$5.00",
"value": 5
},
"rrp_without_tax": {
"formatted": "$12.50",
"value": 12.5
},
"saved": {
"formatted": "$7.50",
"value": 7.5
},
"tax_label": "Tax"
}
},
Am I missing something, or is this functionality not available on Stencil at this time?
Has anyone come up with a creative solution to retrieve this data inside each product card?
Thanks
The product card now allows this to be exposed. It provides an array of all custom fields associated with a product. Information on the product card object is here.
It was a change that had to be made in the BC core app and the fix was released on Sept 20th

Freebase query with inner joins on the statment

Can I create an inner join on free base that uses another query?
I tried to create a join between 2 querys:
The first, select all the artist that the genre is the same as nirvana without nirvana:
[{
"id": null,
"name": null,
"name!=": "nirvana",
"type": "/music/artist",
"genre": [{
"name|=": [
"Punk rock",
"Grunge",
"Alternative rock",
"Rock music",
"Hardcore punk"
]
}]
}]
the second, select all the genre of nirvana:
[{
"name": "nirvana",
"type": "/music/musical_group",
"/music/artist/genre": []
}]
I want to create a query like this but it does not work.
[{
"id": null,
"name": null,
"name!=": "nirvana",
"type": "/music/artist",
"genre": [{
"name|=":
[{
"name": "nirvana",
"type": "/music/musical_group",
"/music/artist/genre": []
}]
}]
}]
I'm not sure why you need to do this all as one query. Since Nirvana's music spans a number of very popular genres your query will probably return thousands of results which will mean that you'll have to make multiple API requests to get all the results either way.
In any case, here's a MQL query that finds all the bands which have at least one music genre in common with Nirvana:
[{
"id": null,
"name": null,
"/music/artist/genre": [{
"id": null,
"name": null,
"!/music/artist/genre": {
"id": "/m/0b1zz",
"name": null
}
}]
}]
The exclamation mark in front of the 2nd genre property means that we want the inverse relationship ie. "music artists with this genre" instead of "music genres for this artist".
Note that I've used the MID (/m/0b1zz) to represent Nirvana. You shouldn't be using names to identify topics in a query since they're not unique. You want results for the Nirvana started by Kurt Cobain in 1987, not just any band named Nirvana.